Home >>Python Built-in Functions >Python exec() Function
Python exec() function is used to execute the given input Python code. It accepts large blocks of code, unlike the eval() function that only accepts a single expression.
Syntax:exec(object, globals, locals)
Parameter | Description |
---|---|
object | This parameter defines a String or a code object. |
globals | This is an optional parameter. It defines a dictionary containing global parameters. |
locals | This is an optional parameter. It defines a dictionary containing local parameters. |
x = 'name = "Jerry"\nprint(name)'
exec(x)
x = 'a = 5 \nb = 10 \nprint(a+b) \nprint(a*b)'
exec(x)