eval() function in Python 3

Preview
You must first complete Read values in Python 3 | input function in Python 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

Description

>>> a=eval(’10’)
>>> print(a)
10
>>> print(type(a))
<class ‘int’>
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

Description

>>> b=eval(‘2.5’)
>>> print(b)
2.5
>>> print(type(b))
<class ‘float’>

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

Description

>>> c=eval(‘True’)
>>> print(c)
True
>>> print(type(c))
<class ‘bool’>
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

Description

>>> d=eval(‘2+3j’)
>>> print(d)
(2+3j)
>>> print(type(d))
<class ‘complex’>

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

Description

>>> a=’2+4-3′
>>> b=eval(a)
>>> print(b)
3
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()

Description

>>> num1=eval(input(‘Enter a number=’))
Enter a number=25
>>> print(num1)
25

I have input integer value 25 in this statement.

eval() function automatically handles the value.

Example 2:

Reading floating point value using eval()

Description

>>> num2=eval(input(‘Enter a real number=’))
Enter a number=2.5
>>> print(num2)
2.5

I have input floating point value 2.5 in this statement.

eval() function automatically handles the values.

Example 3:

Reading complex value using eval()

Description

>>> num3=eval(input(‘Enter a complex number=’))
Enter a complex number=2+5j
>>> print(num3)
2+5j

I have input complex value 2+5j in this statement.

eval() function automatically handles the value.

Example 4:

Reading Boolean  value using eval()

Description

>>> num4=eval(input(‘Enter a boolean value=’))
Enter a boolean value=True
>>> print(num4)
True
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()

Description

>>> t1=eval(input(‘Enter multiple values=’))
Enter multiple values=1,3,5,6,7
>>> t1
(1, 3, 5, 6, 7)

I have input five values 1,3,5,6,7 in this statement.

These values are stored as a tuple in variable t1.

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