in , ,

Python Global Keyword

Python Global Keyword

In this article, you’ll find out about the global keyword, global variable, and when to use global keywords.

Before reading this article, make sure you have got some basics of Python Global, Local and Nonlocal Variables.

In this article, you will learn-

What is the global keyword

In Python, the global keyword allows you to change the variable outside of the present degree. It is used to make a global variable and make changes to the variable in a local context.

Rules of global Keyword

The basic rules for the global keyword in Python are:

  • When we create a variable inside a function, it is local by default.
  • When we define a variable outside of a function, it is global by default. You don’t have to use global keyword.
  • We use global keyword to read and write a global variable inside a function.
  • Use of global keyword outside a function has no effect.

Use of global Keyword

Let’s take an example.

Example 1: Accessing global Variable From Inside a Function

c = 1 # global variable

def add():


When we run the above program, the output will be:


However, we may have some scenarios where we have to change the worldwide variable from inside a function.

Example 2: Modifying Global Variable From Inside the Function

c = 1 # global variable
def add():
    c = c + 2 # increment c by 2


When we run the above program, the output shows an error:

UnboundLocalError: local variable 'c' referenced before assignment

This is because we can just access the global variable but can’t modify it from inside the function.

The solution for this is to use the global keyword.

Example 3: Changing Global Variable From Inside a Function using global

c = 0 # global variable

def add():
    global c
    c = c + 2 # increment by 2
    print("Inside add():", c)

print("In main:", c)

When we run the above program, the output will be:

Inside add(): 2
In main: 2

In the above program, we define c as a global keyword inside the add() function.

Then, we increment the variable c by 1, i.e c = c + 2. After that, we call the add() function. Finally, we print the global variable c.

As we can see, change also occurred on the global variable outside the function, c = 2.

Global Variables Across Python Modules

In Python, we make a single module to hold worldwide factors and offer data across Python modules inside a similar program.

Here is how we can share global variables over the python modules.

Model 4: Share a worldwide Variable Across Python Modules

Make a record, to store global variables

a = 0
b = "empty"

Create a file, to change global variables

import config

config.a = 10
config.b = "alphabet"

Create a file, to test changes in value

import config
import update


When we run the file, the output will be


In the above, we have created three files:, and

The module stores global variables of a and b. In the file, we import the module and modify the values of a and b. Similarly, in the file, we import both and module. Finally, we print and test the values of global variables whether they are changed or not.

Global in Nested Functions

Here is how you can use a global variable in the nested function.

Example 5: Using a Global Variable in Nested Function

def foo():
    x = 20

    def bar():
        global x
        x = 25
    print("Before calling bar: ", x)
    print("Calling bar now")
    print("After calling bar: ", x)

print("x in main: ", x)

The output is:

Before calling bar: 20
Calling bar now
After calling bar: 20
x in main: 25

In the above program, we declared a global variable inside the nested function bar(). Inside foo() function, x has no effect of the global keyword.

Before and after calling bar(), the variable x takes the value of local variable i.e x = 20. Outside of the foo() function, the variable x will take value defined in the bar() function i.e x = 25. This is because we have used global keyword in x to create global variable inside the bar() function (local scope).

If we make any changes inside the bar() function, the changes appear outside the local scope, i.e. foo().

Please feel free to give your comment if you face any difficulty here.

For More Latest Articles Click on Below Link.

salman khan

Written by worldofitech

Leave a Reply

Python Global, Local and Nonlocal variables

Python Global, Local and Nonlocal variables

Python Modules

Python Modules