1.5 Taking user input in Python | Python Full course 2024 #learnpythonin2024 #mlwithakansha

Akansha Yadav shows how to get user input in Python with the input() function, which always returns a string. Convert it to int() or float() to do math, otherwise you'll get errors like "can only concatenate str (not "int")."
Chapters
Ladies and gentlemen, in Python you are not limited to assign the values or store the values. In Python, you can also ask randomly your users to enter some values. So in this video, you will learn about how to take input from the user. To take input from the user, we have a function called input function. So you have to write input in the lowercase and the brackets. input and the bracket is a function. Whenever there is a brackets after any word, we call it a function. Not even we call it, it is actually a function in Python, and it is denoted with the empty brackets. So this input function will do what? It will read a line of a text entered by the user and will return it as a string. Note it down that it will return it as a string only. So the data type of what user is going to enter will by default will be string. It will just read a line of a text entered by user. It can be number, it can be strings. Or characters, anything it can be. So input function is going to do that for you. How to use it? Suppose I'm saying name is equal to input, and here I'm just writing an instruction that so that user can get to know that what exactly. Let me show you. Suppose I'm writing input, I'm going to run it. Let me select the kernel. Okay. So input function will prompt a user to enter something. So here we are getting press Enter to confirm your input or Escape to cancel. So I'm writing 1. So it is already— it has already taken my input. Let's print the name variable. So we're getting 1 as an output. I'm going to run the cell again. Now I'm going to write Roy and enter. I'm going to run it again. Now I have Roy in my variable name, but I was not sure what to write. So in that case, what you can do is, in the same statement, I'm going to make the changes here. In the empty brackets, you can write down what you wanted to instruct your user to do. So in my case, I want my user to write a name. So I'm writing enter your name. So here I am getting prompted with the, with the instruction saying enter your name, press enter to confirm and escape to cancel. So now I can write Roy and so my name will be stored in the variable name. That is how input function works for you. Just keep in mind that it will always return strings. Suppose I'm taking age input. Enter your age. So let's suppose 50. So it is taking my input as an integer only. You can see we are getting the values in the quotations. If I'm going to operate something let's suppose 2. It will perform, you know, mathematical things. So I'm writing 2. It is a num— it is a string, so that is why it is repeating. If it was a number, it has to be like doubled because I'm trying to multiply it. So that is why I am repeating that. Note it down that it is going to be as a string only. You can convert it into any other data type by using those things. Just like if I want the age to be a number only, suppose I'm taking for a game, I'm taking number and I'm asking my user to input, enter any random number. So it is going to ask me a random number. But what if I am going to say I'm going to add something on it? So what will it do? It won't be doing anything because it says that can only concatenate string, not integer. So it means str is string, int is an integer. Str is a short form of a string and integer int is a short form. So what you can do in this case is when you wanted to take number as an input, so you can Write int in the starting of the input and then close the brackets at the end. By this, what you can do is that you will take the values and you can operate anything on it. So I have entered a number, now I'm going to add 50 in it. It can easily, easily do that for you because now what you have done is you have converted your string data into integer using the int keyword. Same way you can convert it into float as well. It means a decimal value. Float, f should be lowercase. 60. So if I will run it, 60 plus this, and I'm getting as a decimal. So this is how you're taking input works. You can work with this easily by converting one data type into another data type. And the next video, I will show you how to convert it one by one.










