exec()
Built-in FunctionPython 2.0+Advanced
Executes a string or code object as Python statements
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
code = "x = 42\nprint(x * 2)" exec(code)
exec() is a built-in function that executes a string or code object as python statements.
Try in PlaygroundTags
builtinfunctioncoremetaprogrammingdynamic
Related Items
eval()
Built-in Function
Evaluates a string as a Python expression and returns the result
compile()
Built-in Function
Compiles source code into a code object for exec() or eval()
importlib
Stdlib — Introspection
Programmatic import system: dynamic imports, reload, hooks
globals()
Built-in Function
Returns a dictionary of the current global symbol table
locals()
Built-in Function
Returns a dictionary of the current local symbol table