- What is input statement?
Python keyboard input: Read dynamic data from the keyboard.
- How to take input from the user in python?
To take input from user we have raw_input()and input() function. But syntax for functions is different in python 2 and python 3.
- In python 2
There are two types of function for reading input from the keyboard
- Raw_input python
Raw_input for reading string input from the user.
This is only for str.
It always takes for str type.
For other type, we required to use the type casting function.
Raw_input python Syntax
X = raw_input(“string data”)
Where X is A variable which stores string data.
X = raw_input(“Enter string ”) => string data
- Input python
Input function is used for reading any type of data from the user.
Data accepted using input function is not considered as str type.
Whatever we provided input it will be accepted.
We are not required to typecast.
Input(“any type of data”)
- Python 3
In python 3 raw_input function is not available.
For taking input we have only one function that is input function.
But In python 3 input() function is different than in python 2.
- Python3 input()
Input() function in python is equal to raw_input() in python 2.
Input() => str
It considers str type, so type casting is required.
Example:- X = input(“enter no”)
Type(X) => str type
Python program to add two numbers
python program to add two numbers
x=input("Enter First number= ")
y=input("Enter second number= ")
- How to read multiple values from the keyboard in a single line?
We can take multiple inputs in a single line but the whole line considered as a string. So for this first, we need to split the input string into multiple values.
Read two numbers from the keyboard in a single line.
Input: 10 20
We receive input in str type. First, we split these two inputs into two parts. For this, we use split() which separates number by considering space in between the string
See the above diagram. We got two numbers 10 and 20.
But these two numbers are of str type. So we need to typecast str to an int type using int() function.
- Complete statement to take two input in single line as
a, b = [int(X) for X in input(“Enter two numbers “).split() ;
- Split() function
Split consider space as separator.
You can use any separator which you need to specify in split().
Read two float values from the keyboard which are specified with, separation and print sum.
Input = 10.5, 10.6
Output sum = 21.1Input statement in pythonPython12A, B =[float(X) for X in input(“Enter two numbers”).split (,) ]print(“sum =”, A+B)
- For taking the different type of data better to go for multiple approaches that is multi-line input.
- The function takes str as input and evaluates based on type.
X = eval(“10+20+30”)
- eval function in python: You can provide expression to eval function.
python eval function with parameterseval functionPython123Ex = input(Enter expression “)Res= eval(Ex)print(res)
- Eval function
If you want to take input as list type, but using the input function it treated as type str. But if you use eval() then it treated as a list and internally type of data considered according to the type of data.
- Python eval function example
eval functionPython12X = eval(input(“Enter data”))print(type(X))
- Take a different type of data in a single line
Yes, it is possible to take different types of data in single line.
Program python eval function
A, B, C = [eval(X) for X in input("Enter 3 values= ").split(",") ]
print("A = ",A)
print("B = ",B)
print("C = " ,C)