Python Global Variables
Python Global Variables
com /python-global-variables/
In this tutorial, you’ll learn what is a global variable in Python and how to use them effectively.
The scope of a global variable is broad. It is accessible in all functions of the same module.
1/9
Python global variables
Example:
In this example, we declared a global variable name with the value ‘Jessa’. The same global variable name is
accessible to everyone, both inside of functions and outside.
# global variable
name = 'Jessa'
def my_func():
# access global variable inside function
print("Name inside function:", name)
my_func()
2/9
# access global variable outside function
print('Name Outside function:', name)
Code language: Python (python)
Output:
Now, let’s see how to use the global variable inside a Python function.
First, create a global variable x and initialize it to 20. The same global variable x is accessible to everyone,
both inside of functions and outside.
Now, create a function with a combination of local variables and global variables.
Create a local variable y And initialize it to 30. A local variable is declared inside the function and is not
accessible from outside it. The local variable’s scope is limited to that function only where it is declared.
In the end, add a global variable x and local variable y to calculate the sum of two variables.
Example:
# global variable
x = 20
def add():
# local variable y
y = 30
print('local variable y=', y)
def sub():
# local variable m
m = 10
print('local variable m=', m)
add()
sub()
3/9
Code language: Python (python)
Output:
local variable y= 30
global variable x= 20
x+y= 50
local variable m= 10
global variable x= 20
x-m= 10
Note: If you create a new local variable inside a function with the same name as a global variable, it will not
override the value of a global variable. Instead, the new variable will be local and can only be used inside the
function. The global variable with the same name will remain unchanged.
Example:
# global variable
x = 20
def my_func():
# local variable with same name
# it will not change global variable x
x = 50
print('local variable x=', x)
my_func()
print('Global variable x=', x)
Code language: Python (python)
Output:
local variable x= 50
local variable x= 100
Global variable x= 20
4/9
let’s see the below code.
# global variable
x = 20
def my_func():
# modify global variable x
x = x + 30
my_func()
Code language: Python (python)
Output:
Execute the above code to change the global variable x’s value. You’ll get an UnboundLocalError because
Python treats x as a local variable, and x is also not defined inside my_func(). i.e, You cannot change or reassign
value to a global variable inside a function just like this.
Use the global keyword to change the value of a global variable inside a function.
Example:
# global variable
x = 20
def my_func():
# modify global variable x using global keyword
global x
x = x + 30
print('global variable x inside a function:', x)
Output:
5/9
in Python, the scope of variables created inside a function is limited to that function. We cannot access the local
variables from outside of the function. Because the scope is local, those variables are not visible outside the
function.
To overcome this limitation, we can use the global keyword to create a global variable inside a function. This
global variable is accessible within and outside the function.
Example:
def my_func():
# create new global variable x using global keyword
global x
x = 100
print('global variable x inside a function:', x)
my_func()
print('global variable x outside a function:', x)
Code language: Python (python)
Output:
Let us see the rules we need to follow to create and use a global keyword.
First, create a special module config.py and create global variables in it.
Now, import the config module in all application modules, then the module becomes available for a global
name.
In Python, to create a module, write Python code in the file, and save that file with the .py extension.
config.py: The config module stores global variables of school and grade
6/9
# global variables
company.py: create a company.py file to import global variables and modify them. In the company.py file, we
import the config.py module and modify the values of the name and address.
employee.py:
Now, in the employee file, we import both config and company modules to test the values of global variables
and whether they are changed.
Output:
As you can see in the output, we successfully accessed and modified the global variables across the files or
modules.
We can also use the globals() function to access and modify the global variables. The globals()
function returns the dictionary of the current global symbol table.
7/9
The global symbol table stores all information related to the program’s global scope and is accessed using the
globals() method.
Function and variables are not part of any class, or functions are stored in a global symbol table.
# global variable
z = 20
def add():
x = 30
y = 20
Output:
The difference between nonlocal and global is that global is used to change global variables, while nonlocal is
used to change variables outside the function. Let us illustrate this with an example.
Example: Access global variables in nested functions using the global keyword
# global variable
a = 10
def outer_fun():
b = 20
def inner_fun():
c = 30
# access outer function variable using nonlocal
nonlocal b
8/9
global a
# add them
a = b + c
Output:
9/9