Python Global and Local Variables

In this tutorial, you will learn about the Global and Local Variables in Python with the help of examples.

In Python there are two main types of variables. They are:

  • Local Variable
  • Global Variable

We will learn about these two in detail.


Local Variables in Python

In Python, a variable declared inside the body of a function or in the local scope is known as a local variable.

Suppose we have the following function:

def add_numbers(n1, n2):
    result = n1 + n2

Let's try to print the result variable from outside the function.

def add_numbers(n1, n2):

#local variable result = n1 + n2
add_numbers(2, 5) print(result)

The above code gives the following error:

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

A variable that is created inside a function is local to it. The result variable is local to the add_numbers() function.

We can only access local variables within the function or its local scope.


Global Variables in Python

In Python, a variable declared outside of the function or in global scope is known as a global variable.

This means that a global variable can be accessed inside or outside of the function. For example,

# global variable message = 'How are you doing?'
def greet(): # print message inside greet() print('message inside function: ', message) greet() # print message outside greet() print('message outside function: ', message)

Output

message inside function:  How are you doing?
message outside function:  How are you doing?

Here, message is a global variable and can be used anywhere in the program.


Global Variable and Local Variable With Same Name

Let's see what happens if a local variable with the same name is defined.

# global variable message = 'Learn Python'
def greet():
#local variable message = 'Take a Break'
print('Message inside function:', message) greet() print('Message outside function:', message)

Output

Message inside function: Take a Break
Message outside function: Learn Python

In the above example, we have initialized two message variables in global and local scopes.

The local message variable is different from the global message variable.

That is why the global message variable is not changed when we assign a different value to the local message variable.


Global keyword

If we need to change the global variable inside the function, we can use the global keyword:

message = 'Learn Python'
def greet():
global message
message = 'Take a Break' print('Message inside function: ', message) greet() print('Message outside function: ', message)

Output

Message inside function:  Take a Break
Message outside function:  Take a Break

The global keyword tells Python that the variable message we are referring to is the global variable.

After we change the global message variable, the changes are reflected in the global scope as well.

Recommended Reading: Python functions

Did you find this article helpful?