Introduction: Python eval() Function
Some Python programs receive expressions as text instead of writing them directly in the code. This is common in calculator programs, scripting tools, configuration systems, and expressions created while the program runs.
Evaluating these expressions by hand needs extra code. The Python eval() Function makes this much easier.
What it is: The eval() function is a built-in Python function that evaluates a Python expression and returns the result. Unlike exec(), it works only with expressions that produce a value.
Take a look at a quick example to see how it works.
You can also see its real-world use cases to learn where it is commonly used.
Now let’s look at its syntax, parameters, return value, and practical examples.
💡 Tip: Use the eval() function only with trusted input because running unknown code can create security risks. To learn more built-in functions, visit the
Python Built-in Functions Learning Guide.
Syntax, Parameters, Return Value and Examples: Python eval() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python eval() Function.
Syntax
eval(expression)
or
eval(expression, globals, locals)
Parameters
| Parameter | Description |
|---|---|
expression |
A Python expression to evaluate. |
globals (optional) |
A dictionary that defines the global namespace available while the expression runs. |
locals (optional) |
A mapping object that defines the local namespace used while the expression runs. |
Return Value
| Return Value | Description |
|---|---|
| Any | Returns the result of the Python expression. |
Quick Example
Here’s a simple example that evaluates a mathematical expression stored as a string.
expression = "10 + 5"
result = eval(expression)
print(result)
# Output:
15
Instead of treating the text like a normal string, eval() treats it as a Python expression and returns the result.
How the Python eval() Function Works
- The
eval()function takes a Python expression. - Python evaluates the expression while the program runs.
- The result is returned.
- Optional global and local namespaces decide where Python finds variables.
- If the expression is incorrect, Python raises an exception.
Examples: Python eval() Function
The examples below show different ways to use the Python eval() Function.
Example 1: Evaluating a Mathematical Expression
expression = "25 + 15"
print(eval(expression))
# Output:
40
Explanation: The string contains a Python expression. The eval() function evaluates it and returns the calculated result.
Example 2: Evaluating an Expression with Variables
x = 12
y = 8
print(eval("x * y"))
# Output:
96
Explanation: Variables in the current program can also be used inside the expression. Python finds x and y before performing the calculation.
Example 3: Evaluating User Input
expression = input("Enter an expression: ")
print(eval(expression))
# Sample Output:
Enter an expression: 7 * 6
42
Explanation: The entered text is treated as a Python expression and evaluated right away. Use this only with trusted input.
Example 4: Using Custom Global Variables
expression = "a + b"
global_values = {
"a": 20,
"b": 15
}
print(eval(expression, global_values))
# Output:
35
Explanation: Instead of using variables from the current program, this example provides its own global namespace.
Example 5: Calling Built-in Functions
print(eval("max(15, 30, 8)"))
# Output:
30
Explanation: The expression can also call built-in Python functions that are available while it runs.
Example 6: Evaluating a Boolean Expression
print(eval("50 > 20"))
# Output:
True
Explanation: The eval() function is not limited to mathematical expressions. It can also evaluate Boolean expressions.
Example 7: Handling an Invalid Expression
try:
print(eval("10 +"))
except SyntaxError as e:
print(e)
# Output:
invalid syntax
Explanation: The expression is unfinished, so Python raises a SyntaxError. Using a try-except block prevents the program from stopping.
Use Cases: When to use the eval() Function
The Python eval() Function is commonly used in situations like these:
- Evaluating mathematical expressions stored as strings.
- Building calculator applications.
- Processing user-defined formulas.
- Evaluating expressions created while the program runs.
- Testing simple expressions.
- Working with safe scripting environments.
Key Takeaways: eval() Function
Here are the main points to remember about the Python eval() Function:
- The
eval()function evaluates a Python expression. - It returns the result.
- Optional global and local namespaces can be provided.
- It works only with expressions, not complete Python statements.
- Incorrect expressions raise an exception.
- Use it only with trusted input because running unknown code can be unsafe.
The eval() function is useful when Python expressions need to be evaluated while the program runs. Use it only when the input comes from a trusted source.