Example 1 — Evaluate Simple Expressions
Evaluate fixed strings with arithmetic, a function call, and a variable.
print(eval("2 + 3 * 4"))
print(eval("max([3, 9, 4])"))
print(eval("'py' * 3"))
x = 10
print(eval("x * 2")) How It Works
- Normal precedence. The string is parsed like code, so multiplication happens first: 2 + 12.
- Built-ins available.
max()works inside the expression. - Any expression. String repetition is an expression too.
- Caller's variables. Without namespaces, the expression can read
xfrom the surrounding code.

