Python eval() Function



The Python eval() function is a built-in function used to evaluate a string as a Python expression and return the result. It takes a string containing a valid Python expression as input, parses it, and execute the code, ultimately returning the result of the expression.

Syntax

Following is the syntax of Python eval() function −

eval(expression, globals=None, locals=None)

Parameters

This function takes the parameters as shown below −

  • expression − It is a string representing a valid Python expression that you want to evaluate.

  • globals (optional) − It is a dictionary containing global variables. If not provided or set to None, it uses the globals namespace of the caller.

  • locals (optional) − It is a dictionary containing local variables. If not provided or set to None, it uses the locals namespace of the caller.

Return Value

This function returns the result of the evaluated expression.

Example

In the following example, we are using the eval() function to evaluate the simple expression "2 + 3", which represents a simple addition −

expression = "2 + 3"
result = eval(expression)
print('The evaluated expression obtained is:',result)

Output

Following is the output of the above code −

The evaluated expression obtained is: 5

Example

Here, we are using the eval() function to calculate the result of the quadratic formula "x2 + 3x + 5" with "x" set to "2" −

formula = "x**2 + 3*x + 5"
x_value = 2
result = eval(formula, {'x': x_value})
print('The evaluated expression obtained is:',result)

Output

Output of the above code is as follows −

The evaluated expression obtained is: 15

Example

We can use the eval() function to access both global and local variables.

In here, we are using the eval() function to calculate the sum of "global_var" and "local_var" −

global_var = 10
local_var = 5
expression = "global_var + local_var"
result = eval(expression, {'global_var': global_var}, {'local_var': local_var})
print('The evaluated expression obtained is:',result)

Output

The result obtained is as shown below −

The evaluated expression obtained is: 15

Example

In this case, we use a list comprehension to generate a list of squares for numbers in the range "0 to 4". After that, we evaluate the expression using the eval() function to obtain the result −

list_expression = "[x**2 for x in range(5)]"
result = eval(list_expression)
print('The evaluated expression obtained is:',result)

Output

Following is the output of the above code −

The evaluated expression obtained is: [0, 1, 4, 9, 16]

Example

In this example, we define a function called "custom_function" that takes a parameter 'x' and calculates a quadratic expression. We then create a string variable called "expression" containing the function call with the argument '3'. To evaluate the expression, we use the eval() function, providing the function and its argument to the globals() dictionary, which includes global variables, along with a dictionary containing the custom function −

def custom_function(x):
   return x**2 + 2*x + 1
expression = "custom_function(3)"
result = eval(expression, globals(), {'custom_function': custom_function})
print('The evaluated expression obtained is:',result)

Output

The result produced is as follows −

The evaluated expression obtained is: 16
python-eval-function.htm
Advertisements