Remember
exec() executes Python statements and returns None. A namespace can hold names created by those statements.
exec() executes Python statements and returns None. A namespace can hold names created by those statements.
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.
namespace = {}
result = exec("number = 3", namespace)
print(result)
print(namespace["number"])Top-level scope
{'number': 3}Object 2 · NoneTypeNoneexec() executes Python statements and returns None. A namespace can hold names created by those statements.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
namespace = {}
result = exec("number = 3", namespace)
print(result)
print(namespace["number"])
|
v
DISPLAYED RESULT
None
3
FINAL BINDINGS
namespace --> object 1 (dict)
result --> object 2 (NoneType)
object 1: {'number': 3}
object 2: NoneChoose 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.
namespace = {}
result = exec("number = 3", namespace)
print(result)
print(namespace["number"])Top-level scope
{'number': 3}Object 2 · NoneTypeNoneThe example has finished. Compare the final output with the values and references above.
None 3
What do you expect this program to print?
namespace = {}
result = exec("number = 3", namespace)
print(result)
print(namespace["number"])exec() FunctionThe exec() function in Python is used to execute dynamically created Python code. It takes a string as input and runs it as Python code.
This is a powerful tool, but it should be used carefully.
exec(object[, globals[, locals]])
object – A string or code object that will be executed as Python code.globals – (Optional) A dictionary to specify the global namespace.locals – (Optional) A dictionary to specify the local namespace.code = "x = 5\ny = 10\nprint('Sum:', x + y)"
exec(code)
Sum: 15
func_code = '''
def greet(name):
print("Hello, " + name)
'''
exec(func_code)
greet("Alice")
Hello, Alice
Never use exec() with untrusted input! It can execute arbitrary code and pose a serious security risk.
global_scope = {}
exec("x = 100", global_scope)
print(global_scope["x"])
100
exec() when eval() would be safer and simplerexec()exec() doesn't return the result of expressionsexec() shows up in questions around code generation, sandboxing, or dynamic behavior. It's powerful but should be avoided in production unless absolutely necessary.
exec() executes Python code passed as a string or code objectNone, modifies scope directlyAsk the user for a math expression and evaluate it using exec().
expr = input("Enter a Python statement (e.g., print(2+3)): ")
exec(expr)
Note: Use eval() if you're only evaluating expressions (not statements).
Connect what you noticed in the example with the idea below.
exec() executes Python statements and returns None. A namespace can hold names created by those statements.
exec() returns the value of the last statement.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.