/  Eval() method in Python Built in Function

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.

  1. Expression:the parsed or the resolved string is entered and evaluated as a python expression.
  2. Locals:this parameter is used a mapping object. Dictionary is an commonly used mapping object.
  3. Globals: It a dictionary.

 

Example-1:

x = 'print(9+8)'
eval(x)

 

Output:

Eval () method in Python Built in Function

 

Example-2:

x = 'print(8-9)'
eval(x)

 

Output:

Eval() method in Python Built in Function

 

Example-3:

x = 'print(17-5)'
eval(x)

 

Output:

Eval () method in Python Built in Function

 

Example-4:

x = 'print(1569*7854)'
eval(x)

 

Output:

Eval () method in Python Built in Function

 

Example-5

x = 8
print(eval('x + 1'))

 

Output:

Eval () method in Python Built in Function