Remember
eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.
eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
expression = "3 + 2"
result = eval(expression)
print(result)Top-level scope
'3 + 2'Object 2 · int5eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
expression = "3 + 2"
result = eval(expression)
print(result)
|
v
DISPLAYED RESULT
5
FINAL BINDINGS
expression --> object 1 (str)
result --> object 2 (int)
object 1: '3 + 2'
object 2: 5Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
expression = "3 + 2"
result = eval(expression)
print(result)Top-level scope
'3 + 2'Object 2 · int5The example has finished. Compare the final output with the values and references above.
5
What do you expect this program to print?
expression = "3 + 2"
result = eval(expression)
print(result)eval() FunctionThe eval() function in Python takes a string and evaluates it as a Python expression. It can be used to run dynamic Python code that's stored as a string.
eval(expression, globals=None, locals=None)
expression – A string that contains a valid Python expression.globals (optional) – A dictionary to define the global namespace.locals (optional) – A dictionary to define the local namespace.result = eval("5 + 10 * 2")
print(result)
25
x = 3
y = 4
expression = "x * y + 2"
print(eval(expression))
14
code = "a + b"
print(eval(code, {"a": 5, "b": 10}))
15
Never use eval() on untrusted input! It can execute arbitrary code, leading to serious security issues.
Example of dangerous input:
eval("__import__('os').system('rm -rf /')")
Instead, use ast.literal_eval() if you just want to safely evaluate basic data types.
SyntaxError.While eval() isn’t commonly used in interviews, understanding it shows that you know Python’s dynamic features and their risks.
eval() evaluates a string as a Python expression.Ask the user to enter a math expression (like 2 + 3 * 4) and print the result using eval():
expr = input("Enter a math expression: ")
print("Result:", eval(expr))
Connect what you noticed in the example with the idea below.
eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.
eval() is a safe way to process arbitrary untrusted user input.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.