Global and Local Variables in Python with Examples

Global and Local Variables in Python

In this post, you will learn about local and global variables in python.

Prerequisites:

Local Variables:

Local variables are the ones that are created inside a block (for example, a function). We cannot modify or access these variables outside the block.

Example:

def print_welcome():
      message = "Welcome to SemicolonSpace"
      print(message)
  
print_welcome()

Output:

Welcome to SemicolonSpace

In the above code, message is a local variable. It is declared inside print_welcome() method. We cannot modify or access it outside the method.

def print_welcome():
      message = "Welcome to SemicolonSpace"
      print(message)
  
print_welcome()
  
print(message)  # Error

Output:

NameError: name 'message' is not defined

We tried to print the value of the message outside print_welcome(). Since we cannot access it outside the method, we got the error.

Global variables:

Global variables are the ones that are created outside a function. We can access them inside any function.

Example:

def print_welcome():
      print(message)
  
message = "Welcome to SemicolonSpace"
print_welcome()

Output:

Welcome to SemicolonSpace

Now, the variable message is a global variable, because we have created it outside the function print_welcome(). We have accessed it inside the function and printed its value.

Important: We cannot update a global variable inside a function. For example, look at the following code.

num1 = 15

def increment():
     num1 = num1 + 1  # Error
     print("num1 inside increment()", num1)
      
increment()
      
print("num1 outside increment()", num1)

Output:

UnboundLocalError: local variable 'num1' referenced before assignment

In the increment() method, we are trying increment num1 which is a global variable. As a result, we got the error. We can fix this in 2 ways.

  1. Using the global keyword
  2. Using globals()

Using the global Keyword:

global keyword allows us to modify global variables in a local context (for example, inside a function).

Example:

num1 = 15

def increment():
     global num1
     num1 = num1 + 1
     print("num1 inside increment()", num1)
      
increment()
      
print("num1 outside increment()", num1)

Output:

num1 inside increment() 16
num1 outside increment() 16

Note: Python allows the declaration of local variables with the same name as global variables.

Example:

num1 = 15

def update():
    num1 = 20
    print("num1 inside update()", num1)
      
update()
      
print("num1 outside update()", num1)

Run the above code. It doesn’t throw any error. It prints the following output:

num1 inside update() 20
num1 outside update() 15

This is Because num1 which is inside the update() method is a brand new variable. It is not related to num1 outside the method. It is a local variable inside the update() method and won’t be considered as the global one.

Using globals():

In Python, globals() returns the dictionary of the current global symbol table. You can read more about it here.

Leave a Comment