Input Through eval() function in Python


☞To take input from the user, we will use input() function and then pass this string into eval() function.

☞eval() function will convert this string into specific type automatically by checking the input value.

☞If we are use eval() function, then there is no need to use datatypes. eval() does it for you.

☞Example :

x=eval(input("Enter a number : "))
print("The answer is ",x)
print(type(x))
print("-----------------------")
y=eval(input("Enter a floating number : "))
print("The answer is ",y)
print(type(y))
print("-----------------------")
z=eval(input("Enter a complex number : "))
print("The answer is ",z)
print(type(z))
print("-----------------------")
a=eval(input("Enter a boolean value : "))
print("The answer is ",a)
print(type(a))
Output
Enter a number : 54
The answer is  54
<class 'int'>
-----------------------
Enter a floating number : 45.44
The answer is  45.44
<class 'float'>
-----------------------
Enter a complex number : 9+9j
The answer is  (9+9j)
<class 'complex'>
-----------------------
Enter a boolean value : True
The answer is  True
<class 'bool'>
Note : We have not taken a string using eval() function because input() function itself take an input as an string, so there is no need to use eval() function if working with string.