Global vs Local Variables
In Python, the scope of a variable determines where it can be accessed and modified. Based on scope, variables are mainly classified into global variables and local variables.
Global Variables
- Global variables are defined outside any function.
- They are accessible throughout the program, including inside functions (for reading).
- By default, a function can read a global variable but cannot modify it directly.
Example: Accessing a Global Variable
a = 10 # global variable
def something():
print("inside :", a)
something()
print("outside :", a)Output:
inside : 10
outside : 10Here, the function can read the global variable a without any issue.
Local Variables
- Local variables are defined inside a function.
- They are accessible only within that function.
- Attempting to access a local variable outside its function results in a NameError.
Example: Local Variable Scope
def something():
a = 15 # local variable
print("inside :", a)
something()
print("outside :", a)Output:
inside : 15
NameError: name 'a' is not definedThe variable a exists only inside the function, so it cannot be accessed outside.

Same Variable Name in Global and Local Scope
- Python allows global and local variables to have the same name.
- They are treated as separate and independent variables.
- Modifying the local variable does not affect the global variable.
Example: Separate Global and Local Variables
a = 10 # global variable
def something():
a = 15 # local variable
print("inside :", a)
something()
print("outside :", a)Output:
inside : 15
outside : 10This demonstrates that the local a shadows the global a inside the function.
Modifying Global Variables Inside a Function
By default, Python treats variables assigned inside a function as local. To modify a global variable from within a function, Python provides two approaches.
1. Using the global Keyword
- The
globalkeyword tells Python that the variable refers to the global scope. - This allows direct modification of the global variable.
a = 10
def something():
global a
a = 20
print("inside :", a)
something()
print("outside :", a)Output:
inside : 20
outside : 202. Using the globals() Function
globals()returns a dictionary of all global variables.- You can modify a global variable by accessing it using its string name as a key.
- This method modifies the global variable, not the local one.
a = 10
def something():
a = 15 # local variable
globals()['a'] = 20
print("inside :", a)
something()
print("outside :", a)Output:
inside : 15
outside : 20Here:
- Local
aremains15 - Global
ais updated to20
Summary
- Local variables exist only within their function scope, any variable defined inside a function is local by default.
- Global variables are defined outside functions and can be accessed (read) anywhere in the program, including inside functions.
- To modify a global variable from within a function, you must explicitly:
- declare it using the global keyword, or
- update it through the globals() dictionary.
- Local and global variables with the same name are treated as separate variables and do not affect each other unless explicitly connected.
This clear separation of scopes enhances code safety, readability, and long-term maintainability.
Written By: Muskan Garg
How is this guide?
Last updated on
