Menu Close

Python exec() Function

Here, we will learn about the Python exec function to execute specified python code. In python, the exec() function provides a facility to execute a dynamically created program or a code object.

In the previous tutorial, we have seen the python eval function to evaluate valid python code. In this guide, we will understand Python exec built-in function along with various examples, so that you can understand the python exec function very easily.

Python exec() function

Python exec function is a built-in python function that is used to execute specified python code.
The main difference between the python exec() function and the python eval() function is, exec function accepts large code but the eval function accepts only a single expression.


The syntax of Python exec method is:-

exec(objects, globals, locals)


Python exec function accepts three parameters.

  • object:- A string or code object.
  • globals:- Optional. A Dictionary containing global parameters.
  • locals:- Optional. A Dictionary containing local parameters.

Return Value

exec function in python does not return any value. It reurns None.

Python exec example

Here we will go through the python exec method along with various examples with easy explanation.


Use python exec function with single parameter.

string = 'x = 10\ny = 15\nprint("X is:- ",x)\nprint("Y is:- ",y)\nprint("Sum of {} and {} is:- {}".format(x,y,x+y))'


X is:-  10
Y is:-  15
Sum of 10 and 15 is:- 25

Global and Local Variables

Python allows us to restrict the use of python built-in functions or methods and attributes by using the global and local parameters. The global parameter overtakes if the local is missing, which means you can that global is the work of both of the fields.

Let’s see example to understand global and local parameters.


from math import *
exec('print(factorial(5))', {})


Traceback (most recent call last):
  File "<string>", line 4, in <module>
File "<string>", line 1, in <module>
NameError: name 'factorial' is not defined

The above example should have printed 120 instead of error but it does not display any output, raise an exception, we have imported all the functions of the math module. We will get an exception because we passed the global parameters.

To solve the above problem, we have to pass the math’s factorial method as a global parameter.

Suppose we want to restrict all the math functions except the factorial method.


from math import *
exec("print(factorial(5))", {'factorial': factorial})

Output will be:- 120

Even you can change the name of the key in dictionary.


from math import *
exec("print(fact(5))", {'fact': factorial})

Output will be:- 120

Let’s what happens, If we pass both global and local parameters in the python exec method.


from math import *
exec("print(dir())", {'built': __builtins__}, {'sum': sum, 'cos': cos})

Output will be:- [‘cos’, ‘sum’]

In the above example, all the built-in functions, as well as sum and cos functions have executed.


In this tutorial, you have learned the python exec function to execute dynamic created python code or code object.
This is the best python built-in function to execute a large amount of code. You always remember that, exec function in python completely different from the eval function in Python.

If this article helped you, please keep visiting for further python built-in functions tutorials.

Other Python built-in functions

For more information:- Click Here

Python float() Function
Python frozenset Function

Was this post helpful?

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *