How input( ) works in Python:
Let us explain it.
You can read or watch the explanation
Let’s learn how to interact with the computer. How to instruct our computer to do things for us. One thing, Whenever you wanna say something to the Computer as a programmer. Try to think like a computer. Think for a second! being in its place and imagine what happens when you write code and try to trace the code. Tracing is the most important part of the program.
You can program well, only when you imagine before doing. Writing the code without tracing is like driving a car without knowing the way. The secret of good programming is tracing the code. If you trace it you can predict the error before running the program. But, You should have a lot more patience during the time of tracing.
We are now going to learn a new function and this function seems to be a mirror reflection of the print function. Everyone knows that print sends data to the console. But this function gets data from the console and this function is input( ) - The name itself is saying that this function is used to take the input.
print( ) will perform only one thing, it just prints the data. In python the speciality of the input is it will perform 2 things at the same time. It prints the output and at the same time it gets the input from the console. It returns the input to the running program.
By this the program can manipulate the data and makes the code truly interactive. This is called data processing. Most of the programs read and process data. Many great and passionate software developers say that “a program which does not get any user input is a deaf program”. Yeah, it’s true we cannot write a program without giving any input. You will give the input some way or the other.
Let us practically see what input() actually does when I’m writing a little “piece” of code using print() and input( ) functions and I’m saving it as inputdemo.py where py is the extension of all python files.
Here I’m printing tell me your name in the first line which means I’m asking the user his/her name.
In the second line I’m trying to take the input of the user and I’m storing it in the variable called name, If you don’t have any idea about the concept of variable… I’ve done a separate video on it, The link will be in the description.
In the third line I’m displaying the output as hello… The name given by the user and how are you…?
Here you can see a very simple code of using input( ) function.
Now how this program runs.
First it displays the line of sentence asking your name
Next line it displays nothing but the cursor is blinking and it is waiting for something. What’s that, your name because it is asking for a name in the first line and in the second line you have to give your name. I’m giving my name here. So the input will be Girl Coder. It will not wait for anything this time and it prints the message using the input that you’ve given to the console.
Hello… Girl Coder, how are you?
Whatever name you give, it will display using that name.
To invoke input( ) function, you don’t need to pass anything as parameters. You can use it without parameters as well and this input is returned to the program through the function's result. To store this function’s result you have to assign a variable like I assigned name here, so that the name is stored here. If you don’t assign then the entered data will be lost.
Here is one more interesting thing about the input function.
It can prompt the user without any help from the print function. Which means the input function itself will print the data and at the same time it will get the input from the console and return to the program.
You can see here I’m altering the code.
output : Hello… Girl Coder, how are you?
Remove the print method and I’m using input to print the same thing that is done by the print method and run the code.
Same thing happened but, there is some difference. Did you notice? When you use the print method the control has gone to the next line. But, here you use the input method and control is in the same line. First it will print the data that you’ve given, and then the input function will do its job.
Let us write a new code.
Here I’m using input which has a parameter of string - Give me a number and you are storing it in a variable called number.
And in the next line let’s create a variable called square for eg. where this variable helps us to get the square of the given number. The output will be, first, it will ask to give you a number and then it generates an error. Yes, you will get an error.
Give me a number2
Traceback (most recent call last):
File "inputdemo.py", line 2, in <module>
square = number ** 2
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Why did we get an error? Because you didn’t specify any literal for the input. If you didn’t specify any literal then python by default sets a literal to the input function which is string. So whatever the input that you give to the console as input will be converted to string. Now, you are trying to find the square of a string. Can we do that? Can we find the “square” of a string? No.
So, it generates some error saying that it is an unsupported operand which means, you cannot perform exponentiation using one string and one integer.Not only with integer, the same thing happens with float.
How to solve these kinds of problems then.
int( ) and float( ) :
Python offers 2 simple functions to solve them. First one is int which takes one argument and it tries to convert it into integer. If it fails then the whole program fails.We will know about it later. Second one is float which also does the same thing but it converts string to float.
You can invoke any of the functions by passing input results directly to them
int(input(string))
float(input(string))
There is no need to use any new variable as an internal storage.Now let’s try to find the area of a triangle. Everyone knows the formula for the area of a triangle.
Area of a triangle = ½ * b * h
So to find the area we basically need 2 things, one is base and the other is height. So,
User will input in the form of an integer and this integer will be stored as a string as you have seen before. So to convert this string to an integer explicitly we are using the int method and this string will be converted to int and stored in the base variable. I hope you’ve understood the speciality of int.
Let us also take height as input.
Same thing is done. User input will be in the format of string and this is converted to integer and stored in the height variable.
the formula for area of the triangle is ½ * base * height
I’m writing ½ as 0.5 so… area = 0.5 * base * height
and now
save it as area.py and run it.
You will get the area of the triangle as output. You can also convert float to integer and integer to float using these methods if you wish to do it. You can also remove the area and directly do the calculation in the print function itself.
If you save and run it you will get the same output.
Python Operators + and * in Strings :
It’s time to introduce 2 arithmetic operators which are + and * .You know about them already. they are not only just adding and multiplying the numbers. They can handle strings as well.
When we apply + sign between 2 strings, it becomes a concatenation operator. It simply attaches both of them and makes them into a single string. It can be used more than once in one expression. During that time it takes left sided binding. It almost acts as an arithmetic sibling. But,The concatenation operator is not commutative.
i.e., “ab” + “ba” is not same as “ba” + “ab”
Let us write a simple code using a concatenation operator by Taking the first and last name of user
Here you don’t need to use any int or float functions for conversion. Because we need the input in string and the default is also string.
take last_name as input
Now print them by concatenating them directly. saying hello…
I saved it as concat.py you save your file on your own as you want.
now run it:
#paste output here
first it asks first name as input… give it and then give last name as input and then press enter.
You will see a message saying hello using your first and last name.
So we can add two strings using the concatenation operator i.e., + operator.
What does this asterisk do?
It repeats the string the number of times specified by the number.
for eg, if we give the name = “coder”
and I print(name * 3)
output: codercodercoder
Asterisk obeys commutative law means even if you print 3 * name you will get the same output.
Up to now we have learned how to convert a string to an integer and a string to float. But, How to convert a number into a string? For that we use a string function called str.
To be honest it can do a lot more than just converting. Coming to the code of area of a triangle we are passing the parameter of number into the print function and we are using a comma in between remaining parameters instead we can convert it into string and use just + in between them instead of commas. You will get the same output.
So far we know about :
- Input() method
- int, float and str methods and
- string operators
See you in the next blog.
Post a Comment
0Comments