Python Global and Local Variables

In this tutorial, you will two very important variable types in Python which are- Global and Local variables. First of all, we will take a look at global variables and then focus on local variables.

Global Variables in Python

Variables that are declared outside a function and are accessible to all the functions are called global variables. In other words, you can access global variables outside and inside a function.

Example: How to create global variables in Python?

x = 10

def display():
   print('Value of x inside function: ', x)

display()
print('Value of x outside function: ', x)

Output of the above program

Value of x inside function: 10
Value of x outside function: 10

In the above code, we have declared a global variable x and displayed its value inside display() function.

What will happen if you try to change the value of x inside a function?

x = 10

def display():
   x = x + 15
   print('Value of x inside function: ', x)

display()

Output of the above program

UnboundLocalError: local variable 'x' referenced before assignment

If you try to change the value of the global variable inside a function, then you will get UnboundLocalError error. You will get this error because Python considers variable x as a local variable and x is not defined inside display() function. To get around this problem, you need to declare it using global keyword.

How to use global keyword inside a function?

x = 10

def display():
   global x
   x = x + 15
   print('Value of x inside function: ', x)

display()
print('Value of x outside function: ', x)

Output of the above program

Value of x inside function: 25
Value of x outside function: 25

Local Variables in Python

Local variables are declared inside a function and are only accessible to that function. In other words, local variables cannot be accessed by other functions. If in case you try to access local variable outside a function, then you will get NameError error.

Example: How to create local variables in Python?

def show():
  x = "I am a local variable"
  print('Value of x: ', x)

show()
print('Value of x outside function: ', x)

Output of the above program

Value of x: I am a local variable
NameError: name 'x' is not defined

As you can see above that we cannot access the local variable outside the function.

Example: Global and Local variables in same program

x = "I am a global variable"

def show():
  # Variable 'x' is redefined as a local variable.
  x = "I am a local variable"
  print('Value of x: ', x)

def display():
  global x
  x = "I am still a Global variable"
  print('Value of x: ', x)
  y = "I am a local variable"
  print('Value of y: ', y)

show()
print('Value of x: ', x)
display()
print('Value of x: ', x)

Output of the above program

Value of x:  I am a local variable
Value of x:  I am a global variable
Value of x:  I am still a Global variable
Value of y:  I am a local variable
Value of x:  I am still a Global variable

show() function has redefined global variable x as a local variable. So modifying x will not change the value of global variable x. After show() function, we have displayed the value of x to check whether its value is changed or not.

To modify global variable x inside display() function, we have declared it global and then changed its value. The updated value is displayed inside as well as outside display() function.