Skip to content

eval()

The built-in eval() function allows you to dynamically evaluate Python expressions from a string or compiled code object and returns the result of the evaluated expression:

Language: Python
>>> eval("print('Hello, World!')")
Hello, World!

eval() Signature

Language: Python Syntax
eval(expression, /, globals=None, locals=None)

Arguments

Argument Description Default Value
expression A string or a compiled code object representing a Python expression to evaluate Required argument
globals A dictionary representing the global namespace to use during evaluation None
locals A dictionary representing the local namespace to use during evaluation None

Note: If locals isn’t provided, then it defaults to globals. If both arguments are omitted, then expression is executed with the global and local names in the environment where you called eval(). Only expression is a positional-only argument. Since Python 3.13, you can pass globals and locals either positionally or as keyword arguments.

Return Value

  • The result and its data type depend on the evaluated expression.

eval() Examples

With a mathematical expression as an argument:

Language: Python
>>> eval("2 + 2")
4

With a function call as an argument:

Language: Python
>>> eval("sum([1, 2, 3])")
6

With the globals argument:

Language: Python
>>> x = 100  # A global variable
>>> eval("x + 100", {"x": x})
200

With the locals argument:

Language: Python
>>> eval("x + 100", {}, {"x": 100})
200

eval() Common Use Cases

The most common use cases for the eval() function include:

  • Evaluating mathematical expressions from user input or strings
  • Dynamically calling functions based on string input
  • Parsing and evaluating logical expressions

Note: Python’s eval() has important security implications. It’s considered insecure because it could allow your users to dynamically execute arbitrary Python code. So, you should avoid using this function when you get the input expressions from an untrusted source.

eval() Real-World Example

You can use eval() to create an interactive command-line math expressions evaluator that processes and evaluates expressions provided by the user:

Language: Python
>>> def eval_expression(expression):
...     code = compile(expression, "<string>", "eval")
...     if code.co_names:
...         raise NameError(f"Use of names not allowed")
...     return eval(code, {"__builtins__": {}}, {})
...

>>> eval_expression("3 + 4 * 5 + 25 / 2")
35.5

>>> eval_expression("sum([1, 2, 3])")
Traceback (most recent call last):
    ...
NameError: Use of names not allowed

In this example, eval() evaluates the user’s mathematical expression safely by restricting access to built-in functions, minimizing potential security risks.

Python's eval(): Evaluating Expressions Dynamically

Tutorial

Python eval(): Evaluate Expressions Dynamically

In this step-by-step tutorial, you'll learn how Python's eval() works and how to use it effectively in your programs. Additionally, you'll learn how to minimize the security risks associated to the use of eval().

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated June 14, 2026 • Reviewed by Dan Bader
Morty Proxy This is a proxified and sanitized view of the page, visit original site.