Python eval() Function – Tutorial with Examples

The eval() function in Python is a built-in function that allows the evaluation of a valid expression passed as a string argument. The expression can be a mathematical expression, a string expression, a list expression, etc. The eval() function evaluates the expression and returns the result, which can be used for further computation or stored as a value in a variable.

Syntax

eval(expression[, globals[, locals]])

Parameters

  • expression – The string expression to be evaluated.
  • globals (optional) – A dictionary of global variables that will be available during the evaluation of the expression. If not provided, the globals from the calling scope will be used.
  • locals (optional) – A dictionary of local variables that will be available during the evaluation of the expression. If not provided, the locals from the calling scope will be used.

Return Value

The eval() function returns the result of the evaluated expression, which can be of any type, such as int, float, string, list, etc.

Examples

Example 1: Basic Usage

expression = "10 + 20"
result = eval(expression)
print(result)

Output:

30

In this example, a string expression is passed to the eval() function, which evaluates the expression and returns the result, which is then stored in the variable result. The value of result is then printed to the console, demonstrating that the expression has been evaluated correctly.

Example 2: Evaluating a List Expression

expression = "[1, 2, 3, 4, 5]"
result = eval(expression)
print(result)

Output:

[1, 2, 3, 4, 5]

In this example, a string expression is passed to the eval() function, which evaluates the expression and returns the result, which is then stored in the variable result. The value of result is then printed to the console, demonstrating that the expression has been evaluated correctly and the result is a list.

Example 3: Using Local Variables

x = 10
expression = "x * 2"
result = eval(expression, {}, locals())
print(result)

Output:

20

In this example, a string expression is passed to the eval() function with a dictionary of local variables passed as the third argument. The expression uses the value of the local variable x, which has been defined earlier and its value is 10. The locals() function returns a dictionary of the current local variables, and in this case, it returns the value of x as 10. The eval() function then evaluates the expression, using the value of x from the dictionary of local variables, and returns the result, which is 20. The value of result is then printed to the console, demonstrating that the expression has been evaluated correctly, using the local variable x.

Use Cases

The eval() function is useful in a number of different scenarios. Some of the most common use cases include:

  • Evaluating mathematical expressions: The eval() function can be used to evaluate mathematical expressions, as shown in Example 1.
  • Evaluating code dynamically: The eval() function can be used to evaluate code dynamically, based on input from the user. For example, a user could enter a mathematical expression, which can then be evaluated using the eval() function and the result displayed to the user.
  • Loading data from a file: The eval() function can be used to load data from a file, which is stored as a string expression. The data can then be evaluated using the eval() function, and the result can be used for further computation or stored as a value in a variable.

However, it is important to note that the eval() function is a potential security risk, as it allows the execution of any valid Python expression, including malicious code. It is recommended to use ast.literal_eval() instead, as it only allows the evaluation of literals, such as numbers and strings, and raises a ValueError for anything else.

Conclusion

In conclusion, the eval() function in Python is a powerful and versatile function that allows the evaluation of expressions passed as a string argument. It can be used in a variety of different scenarios, including evaluating mathematical expressions, evaluating code dynamically, and loading data from a file. However, it is important to be cautious when using the eval() function, as it is a potential security risk, and it is recommended to use ast.literal_eval() instead for safer evaluation of expressions.

Leave a Reply

Your email address will not be published. Required fields are marked *