Python exec() function

Example

Execute Code Block:

x = 'name = "Bill"\nprint(name)'
exec(x)

Running Instance

Definition and Usage

The exec() function executes the specified Python code.

The exec() function accepts a large number of code blocks, which is different from the eval() function that only accepts a single expression.

Syntax

exec(object, globals, locals)

Parameter Value

Parameter Description
object String or code object.
globals Optional. A dictionary containing global parameters.
locals Optional. A dictionary containing local parameters.