The main job of most of the Programming Language is:
- Taking some input (may be from a user or from other sources).
- Processing the input accordingly.
- Generate and present the output.
There is no violation of the above-mentioned points in Python also. To take input from the user, there is a function ‘input()’ and to generate output, there is another function ‘print()’. In this tutorial, we will discuss these two functions in brief.
Taking input from the user
In Python, there is a function ‘input()’ (it is built in function in Python) to take input from the user. If the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return key. The input of the user will be interpreted. As an example, If the user gives an integer value, the input function returns this integer value. If the user, on the other hand, inputs a list, the function will return a list. But we have to keep some points in mind. The points will be discussed later in this tutorial.
Now we can take input in two ways.
- We take the input from the user but did not store it.
- We take the input from the user and store it in some variable for future use.
Case – 1:
If we have to just take a input and we don’t have to store it then the prototype of the function call will be:
input(“<Your Statement will be here>”)
Note: As argument for the input function a String should be passed (it is not mandatory to pass a String always, but if you want to tell the user that what type of input he has to provide and how many inputs he needs to give then it will be useful,) which will be printed at the time of taking the input. As example:
# If we don't pass a String as argument >>> input() 5 # Note that while taking input nothing is printed '5' # After taking the input, # the input() function will immediately return the input to the screen
# If we pass a String argument >>> input("Enter an integer value: ") Enter an integer value: 5 # Note that our String argument has been printed to # the screen while taking input '5' # input() function immediately returns the input value
In the above shown two ways we can take input from the user.
Let’s talk about the second case, where we have to take input from the user and we will store that value in a variable for future use.
Case – 2:
If we have to take input from the user and we have to store that value in a variable for future use, then we have to modify the above two processes a little bit. Here the general prototype will be:
<variable name>=input(“<Your Statement will be here>”)
The same thing we also have to keep in mind here, as argument for the input function a String should be passed (it is not mandatory to pass a String always, but if you want to tell the user that what type of input he has to provide and how many inputs he needs to give then it will be useful,) which will be printed at the time of taking the input. As example:
# If we don't pass a String as argument >>> a=input() # We are taking variable 'a' 5 # Note that while taking input nothing is printed # Note that in this case as we are storing the input value to a variable, # the value will not be printed automatically, we need to print the variable # manually to check the value >>> a '5'
# If we pass a String argument >>> a=input("Enter an integer value: ") Enter an integer value: 5 # Note that our String argument has been printed to # the screen while taking input # Note that in this case as we are storing the input value to a variable, # the value will not be printed automatically, we need to print the variable # manually to check the value >>> a '5'
Now, we should keep in mind a few more things while we are using input() function.
In the first part of this tutorial, we came to know that if the user gives an integer value, the input function returns this integer value. If the user, on the other hand, inputs a list, the function will return a list. This statement is partially true. Because it is correct that the input() function will return an integer or a list respectively but not as an integer or list-type object. The input() function will return them in their string representation. As example:
# We are giving an integer input >>> a=input() 5 >>> type(a) <class 'str'> # We are giving an float input >>> b=input() 5.0 >>> type(b) <class 'str'> # We are giving an boolean input >>> c=input() True >>> type(c) <class 'str'> # We are giving an String input >>> d=input() Rajesh >>> type(d) <class 'str'> # We are giving an character input >>> e=input() k >>> type(e) <class 'str'>
So we can see that, though we have given integer, float, boolean, string and character input respectively, each time we are getting their string representation.
Now, if we convert the string type object into our required type then we need to typecast the value accordingly. As example:
# We are typecasting to integer >>> a=int(input()) 5 >>> type(a) <class 'int'> # We are typecasting to float >>> b=float(input()) 5.0 >>> type(b) <class 'float'> # We are typecasting to boolean >>> c=bool(input()) True >>> type(c) <class 'bool'> # Observe the below two cases carefully, we will discuss an important point # We are typecasting to string >>> d=str(input()) Rajesh >>> type(d) <class 'str'> # We are typecasting to character >>> e=char(input()) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'char' is not defined
Note: In the first case (where we are typecasting to string), it is basically useless to typecast an input value to a string. Because by default input() function returns the string representation of the input value. So there is no point in again typecasting them to string.
And, in the second case, we got an error saying “NameError: name ‘char’ is not defined”, because there is no concept of character in Python. Everything within single quotes(‘ ‘) or double quotes(” “) is a string in Python.
This was all about input() function in Python. I hope you like this tutorial.