eval() function in Python 3

You must first complete Display output in Python 3 | print() function in Python 3 before viewing this Lesson

eval ( ) Function in Python

The eval ( ) function is used to convert a string value to numeric value.

Example 1:.

Convert string value to integer value
>>> a=eval(’10’)
>>> print(a)
10
>>> print(type(a))
<class ‘int’>
Description
eval() function converts string value ‘10’ into  integer value 10.
That is why type(a) is returned as int.

Example 2:

Convert string value to floating point value
>>> b=eval(‘2.5’)
>>> print(b)
2.5
>>> print(type(b))
<class ‘float’>
Description

eval() function converts string value ‘2.5’ into  floating point value 2.5.

That is why type(b) is returned as float



Example 3:

Convert string value to boolean value
>>> c=eval(‘True’)
>>> print(c)
True
>>> print(type(c))
<class ‘bool’>
Description
eval() function converts string value ‘True’ into  Boolean value True. That is why type(c) is returned as bool.**bool is treated integer by Python

Example 4:

Convert string value to complex value
>>> d=eval(‘2+3j’)
>>> print(d)
(2+3j)
>>> print(type(d))
<class ‘complex’>
Description

eval() function converts string value ‘2+3j’ into  complex value 2+3j.

That is why type(d) is returned as complex. 



Evaluating expression using eval()

We can evaluate numeric expressions given in the form of a string into number using eval() function.

Example

Evaluating a string expression
>>> a=’2+4-3′
>>> b=eval(a)
>>> print(b)
3
Description
Numeric expression ‘2+4-3’ is a string expression. eval() function converts this expression into number and returns the result 3.



Reading numeric values using eval()

We can use eval () function with input() function to read numeric values. We can put integer, real, complex or boolean value using this statement. eval() function will automatically handle the value.

Example 1:

Reading integer value using eval()
>>> num1=eval(input(‘Enter a number=’))
Enter a number=25
>>> print(num1)
25
Description
I have input integer value 25 in this statement. eval() function automatically handles the value.

Example 2:

Reading floating point value using eval()
>>> num2=eval(input(‘Enter a real number=’))
Enter a number=2.5
>>> print(num2)
2.5
Description
I have input floating point value 2.5 in this statement. eval() function automatically handles the values.

Example 3:

Reading complex value using eval()
>>> num3=eval(input(‘Enter a complex number=’))
Enter a complex number=2+5j
>>> print(num3)
2+5j
Description
I have input complex value 2+5j in this statement. eval() function automatically handles the value.

Example 4:

Reading Boolean  value using eval()
>>> num4=eval(input(‘Enter a boolean value=’))
Enter a boolean value=True
>>> print(num4)
True
Description
I have input boolean value True in this statement. eval() function will automatically handle the value.

 Reading tuple using eval()

We can use eval () function with input() function to read values of a tuple. We can put multiple values separated by commas. They will be automatically stored as a tuple.

Example:

Reading integer value using eval()
>>> t1=eval(input(‘Enter multiple values=’))
Enter multiple values=1,3,5,6,7
>>> t1
(1, 3, 5, 6, 7)
Description
I have input five values 1,3,5,6,7 in this statement. These values are stored as a tuple in variable t1.




Spread the love
Lesson tags: eval() function in python, read list in python, read numeric values in python, read tuple in python
Back to: Python Programming Tutorial
Spread the love