Python Global Keyword: How to Use Global Variable in Python

In Python, a global keyword allows you to modify the variable outside of the current scope. In this post, you’ll learn about the global keyword, global variable, and when to use global keywords in Python.

Python Global Keyword

The global keyword is used to create the global variable and make changes to a variable in a local context. The basic rules for a global keyword in Python are the following.

  1. When we create a variable inside the function, it is local by default.
  2. When we define a variable outside of the function, it’s global by default. You don’t have to use the** global** keyword.
  3. We use the global keyword to read and write a global variable inside the function.
  4. The use of a global keyword outside the function does not affect.

Access and Modify Python Global Variable

First let's try to access a global variable from the inside of a function,

b = 3 # global variable

def add():
    print(b)

add()

Output:

3

Here, we can see that we have accessed a global variable from the inside of a function.

However, if we try to modify the global variable from inside a function as:

# global variable
b = 1 

def add():

     # increment b by 4
    b = b + 4

    print(b)

add()

Output;

UnboundLocalError: cannot access local variable 'b' where it is not associated with a value

This is because we can only access the global variable but cannot modify it from inside the function.

Example: Changing Global Variable From Inside a Function using global

# global variable
b = 2 

def add():

    # use of global keyword
    global b

    # increment b by 2
    b = b + 3 

    print(b)

add()

Output:

5

In the above example, we have defined b as the global keyword inside add().

Then, we have incremented the variable b by 3, i.e b = b + 3.

As we can see while calling add(), the value of global variable c is modified from 2 to 5.

Example: Global in Nested Functions

In Python, we can also use the global keyword in a nested function. For example,

def outer_function():
    num = 20

    def inner_function():
        global num
        num = 25
    
    print("Before calling inner_function(): ", num)
    inner_function()
    print("After calling inner_function(): ", num)

outer_function()
print("Outside both function: ", num)

Output:

Before calling inner_function():  20
After calling inner_function():  20
Outside both function:  25

In the above example, we declared a global variable inside the nested function inner_function().

Inside outer_function(), num has no effect of the global keyword.

Before and after calling inner_function(), num takes the value of the local variable i.e num = 20.

Outside of the outer_function() function, num will take the value defined in the inner_function() function i.e x = 25.

This is because we have used the global keyword in num to create a global variable inside the inner_function() function (local scope).

So, if we make any changes inside the inner_function() function, the changes appear outside the local scope, i.e. outer_function().

Thanks for reading !!!

#python #python global

Python Global Keyword: How to Use Global Variable in Python
2.20 GEEK