Eval() method in Python Built in Function
Eval method in built in function evaluates the python code which is run in the program and then executes an expression.
Syntax:
eval(expression[, globals[, locals]])
Parameter
Eval has three parameters.
- Expression:the parsed or the resolved string is entered and evaluated as a python expression.
- Locals:this parameter is used a mapping object. Dictionary is an commonly used mapping object.
- Globals: It a dictionary.
Example-1:
x = 'print(9+8)' eval(x)
Output:
Example-2:
x = 'print(8-9)' eval(x)
Output:
Example-3:
x = 'print(17-5)' eval(x)
Output:
Example-4:
x = 'print(1569*7854)' eval(x)
Output:
Example-5
x = 8 print(eval('x + 1'))
Output: