× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Python Global Keyword

In this article, you’ll learn 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.

What is the global keyword
In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.

Rules of global Keyword
The basic rules for 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 need to modify the global 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 only access the global variable but cannot 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 2, 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 create a single module config.py to hold global variables and share information across Python modules within the same program.
Here is how we can share global variables across the python modules.
Example 4: Share a global Variable Across Python Modules
Create a config.py file, to store global variables
a = 0
b = "empty"
Create a update.py file, to change global variables
import config

config.a = 10
config.b = "alphabet"
Create a main.py file, to test changes in value
import config
import update

When we run the main.py file, the output will be
In the above, we have created three files: config.py, update.py, and main.py.
The module config.py stores global variables of a and b. In the update.py file, we import the config.py module and modify the values of a and b. Similarly, in the main.py file, we import both config.py and update.py 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 nested function. Example 5: Using a Global Variable in Nested Function
def myfun():
    x = 20

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

print("x in main: ", x)

The output is :
Before calling myfuninner: 20
Calling myfuninner now
After calling myfuninner: 20
x in main: 25
In the above program, we declared a global variable inside the nested function myfuninner(). Inside myfun() function, x has no effect of the global keyword.
Before and after calling myfuninner(), the variable x takes the value of local variable i.e x = 20. Outside of the myfun() function, the variable x will take value defined in the myfuninner() function i.e x = 25. This is because we have used global keyword in x to create global variable inside the myfuninner() function (local scope).
If we make any changes inside the myfuninner() function, the changes appear outside the local scope, i.e. myfun().

Python Global Keyword