Python Eval


Eval function

The eval() function evaluates an expression and returns the value of expression. The return value can be number or string.

Examples

Example 1

>>> a = 2
>>> b = 3
>>> eval('a + b')
5
>>> eval('a + 1')
3
>>> 

Example 2

>>> x = 'abc'
>>> y = 'def'
>>> eval('x + y')
'abcdef'
>>> eval('x - y')
Traceback (most recent call last):
  File "", line 1, in 
    eval('x - y')
  File "", line 1, in 
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>>