#18 Python Tutorial for Beginners | User input in Python | Command Line Input

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Erwin Wendy and let's continue with the series on Python now the last video we have talked about how can we use PI job right as I mentioned before you can use any IDE which you like okay every idea almost began same way now in this video we'll do something which you are waiting from a long time and which is how to get the input from a user sit trust me when you are building a project you are doing it for the client right and of course you are expecting a client input until this point we are working with other own numbers we should be looking for the user input right how do we do that so thing is in Python whatever we have done till now is done with the help of functions example if you want to print something print function if you want to do some calculation like finding the sum of the list or finding the minimum value of it or maybe whatever functions whatever operations we have done till now we have used functions right for input as well we have an input function name itself says it right now so we shop using five-year we of course will be adding two numbers but there is not using five we will be using a function called as input okay so a simple and then for y as well we will be saying input so for X also we have input for Y also we have input now will it work that's a question I will try it okay so I will right click here I will say run my code okay it works you can see there is no error okay first step is done there is no error but think about this you go to the pencil and you are waiting for something because till this point we were just doing something and you are getting the output this is the first time your cancer is not saying anything the thing is your pencil is waiting for you I mean now when I say you are talking about the user a concern is waiting for the user to give the input now cancer I don't know that user don't know how to do now so user doesn't know what to do a console you don't know that user is waiting so a pencil should notify user hey user I am expecting a value from you and of course Spencer is not that smart to ask for it so as a programmer you need to do it so I will stop the execution I will say here user enter the value so of course it's not compulsory but then it's a ghost gesture right I will say enter enter first number okay and the same thing goes for y as I mentioned is not composite to type the message but then it's better always better enter a second number okay so we go I think the first might a second number and now I will read up the code right click run and you can see it is asking for the value of now I user know something is happening so user will come here and say user will say I want to add nine and I want to enter 5 9 plus 5 will give you 14 right I will say enter oh that's where we got 9 space 5 that's bad the thing is what is happening here is if you have observed I was also giving a space in between maybe that's the issue I will run this again and this time I will give a value which is 9 without space and 5 without space this time at least we'll be getting 14 right hopefully I'll Center again they got 95 that's bad what's wrong here so when you want to work with a function called as input it will always give you this string type if even but we also have a type which is STR so it will always give you the value in STR format what you want is integer wise because you are adding two values and if you want to add two values it should be integer example if I if I bring the type of X in this case of course after after fetching the back after fetching the input I will say run this code once again because it is waiting for the input of it say 900 meters say in turn nine you can see the type of nine is STR it's an integer right but I think is by default input function will convert everything as a string now that means you need to convert this value before assigning the value so what you can also do is you can take another well any other variable like a and you can convert the value of x into integer because x is string so you can say int and you can pass X right we have the we have seen that how do you convert from string to integer of wrong different function different function so this is how you convert a string into integer and then you can add you can simply say a quiche of X you can say a and for Y as well the same thing because Y is also string so I would say B is equal to int of Y mm-hmm and now we'll say B now it should work we are expecting a value which is 14 if this will not work I was I will stop the video and run okay so let's enter 9 and at let's enter 5 hopefully it will work I would say enter of it work finally so you can still continue can still watch the video so yeah so it was 14 right so it is working so the issue was by default input gives your string format we need to convert that into format which we want example if you want float simply use float function if you want integer you can use integer function okay this works but then don't you think we are wasting some lines of code here example I could have used input here itself I mean int here itself because the input gives you strength so we are converting that string into integer so the type of X is now integer so we don't need a there right so it's always better to save the number of lines so here is what I will say ain't and they should work instead of a and B I will say X plus y finally it should work now without a and B of course that was our main agenda right now it is asking for the value this time I will go for some other values you know 7 plus 3 it should give you 10 and it works so this thing is working now in fact the same code which we are doing here on PyCharm will work on IDL editor as well okay that works so let's do something extra now what I will do is I want to take the input from a user in a CAD format because see we don't have a cache format in Python right we have and we have load we have string how do we take a can example we can do that actually you can say X is equal to or maybe this time we'll use CH because that makes sense I want to character right so see H is equal to input of course we don't need to convert that into integer because it's character and you will say enter the entire character enter cat now see you can use double quotes or single quote both works as you know and then I'll print CH yeah this should work let's try I will just run this code we're waiting for the character we'll use P enter it works you can see we can take character as well but here's a trick the trick is what he finds a defiant up PQ R of course right I am asked for a character but or maybe your users don't know that are you you are expecting a character maybe use by mistake user typing multiple characters in this case look at the output we are expecting a character user enters entire string but still we still contact all right if you want to have that control to print only one character you can use index values we have done that right if you remember when we are talking about strings we have done this we can use index in the magic and fetch first character this should work let's try how it's a PQR and you can see record P right this is fun in fact the next chord is amazing okay it's more fun what if I don't even because in C H we still have PQ 1 what if you if you don't want PQ and you only want P so we have a trickier what if while fetching the input only you will specify that hey won't you pick up I already want the first actor say input will fetch the entire string input will fetch P Q R but it will assign only P 2 CH let's try how's the P Q R enter you can see that you got B so again you can play with the codes right and that's why programming is awesome there's a concept of syntax plus there is also a concept of fun so you just play with it it's like playing a game with with programming language in fact since we are working with input we can do something extra we can also use a function called as eval which will evaluate the expression which you are passing example let's say if I give the input you let me just run this code if I give the input as 2 plus 6 minus 2 or maybe 6 minus 1 can we do it can we do it here because if when you pass 2 plus 6 minus 1 is not normal integer it is not normal a string it is it's something different why it's an expression and to use that of course you've got to here because we were fetching on the first item if you want to evaluate that what I will do is I will say result so there's a function called as eval of course we will not be using index iam so it is a function for this eval so in eval function you are passing the data which which is which are getting from the user so it's the enter an expression right so when a user in terms the expression it will evaluate and it will give you the output let's do that here I will print result let's try I hope it will work ok so let's give the expression here I would say 2 plus 6 6 minus 1 enter yes it's working ok you can see record 7 so that's the exciting thing about functions and also in Python we have so many functions to work with now since we are talking about input we can do something extra as well what if you want to take the input from the command line now when I say command line let's say let's go back to this code if you remember this code here we was running my code P by right we will be a page where we have printed only had a world X Y once again you can see but there was some code we are printing 11 but what if I want to pass a value from here example I will say Python and here I will enter my code dot py nothing values in my code that is verify my code here so this is my coil okay so this is this is so I went to texture by 0.8 this 5 which is my code and you can see the values which is 5 and 6 if you want if you want you can put some user you can use input functions but I want to pass the values when I'm running this so if you see CMD here when I am doing the code I want to pass a value which is our six and two I want to add these two numbers is it possible can you pass a value from here yes you can but are you accepting the value no beads so even if I done this quote you can see we got 11 so we should be changing this value instead of using 5 we can use a special keyword which is called as AG RV now AG RV will be having multiple values not one it will be unleashed so example if you are passing one value it will be one value if you are passing two values below two values do before passing 10 values you it will be having ten values and if you want to access a particular value you need to use index number okay now what index number so index number zero is a file name so if I go back to my command round you can see when I say Python and then after that I'm entering a file name now this is your index number zero 6 is index number one and two is index none - so if you want to fetch it you will be using a RG V and in this packet you will say this is one why not zero because zero is for file name right so we'll use one and two and now you can add these two values let's verify let's go back to our Python plant and I would say enter oh there is an error it says a RGB is not defined okay the thing is it belongs top model consists so we have to say import sis and then it will say sis dot argv since dot a RGB now it should work hopefully go back to python enter oh we got 6 + 2 or get the same problem it will give you a string right so we have to convert that into ain't my bad but you learn from your mistakes let's go back and Center or we go 8-8 yeah it's working so that's how you work with input so you can do whatever you want take the input and find the answer you will find the question in the assignment section which is at the end of this video so make sure that you subscribe which I know so that you can enjoy the entire Python series and also like the video if you have liked it thank you so much for watching everyone you
Info
Channel: Telusko
Views: 919,151
Rating: 4.92488 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, beginner, user, input, output, user input, command line input, cmd, command line, how to, print, function, message, type, data, when, how, use, index, eval, pass, command, line, ardument
Id: 4OX49nLNPEE
Channel Id: undefined
Length: 11min 58sec (718 seconds)
Published: Tue Jul 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.