understanding the nature of Global and local Variables in Python

A Local variable is defined inside a function. Its existence is confined to that function's "block." Once the function finishes executing, the local variable is destroyed, and its data is no longer accessible.
def greet():
message = "Hello, Local Scope!" # This is a local variable
print(message)
greet()
# print(message) # This would raise a NameError
Scope: Limited to the function greet().
Accessibility: Cannot be accessed from outside the function.
A Global variable is defined outside of any function, usually at the top level of the script. These variables are accessible from anywhere in the code—both inside and outside of functions.
total_score = 100 # This is a global variable
def show_score():
print(f"Your current score is: {total_score}")
show_score()
print(f"Global access: {total_score}")
Scope: The entire program.
Accessibility: Accessible by any function in the file.
global KeywordBy default, if you try to modify a global variable inside a function, Python treats it as creating a new local variable with the same name. To modify the actual global variable, you must use the global keyword.
count = 0
def increment():
global count # Tells Python to use the global 'count'
count += 1
print(f"Inside function: {count}")
increment()
print(f"Outside function: {count}")
| Feature | Local Variable | Global Variable |
| Declaration | Inside a function. | Outside all functions. |
| Lifetime | Exists only while the function runs. | Exists until the program ends. |
| Accessibility | Only within that specific function. | Throughout the entire script. |
| Modification | Direct access within the function. | Requires the global keyword to modify. |
a = 5
print("I am variable named a at the top of the program my value is", a)
def myfun():
# This function simply reads the global variable 'a'
print("the value of a is", a)
myfun()
def myfun2():
# This creates a NEW local variable 'a' that only exists inside this function
a = 4
print("i am value of a inside myfun2", a)
myfun2()
print("Outside myfun2, the global a is still", a)
def myfun3():
global a # This tells Python we are talking about the 'a' at the very top
a = 3 # This actually CHANGES the global variable
print("I used the global keyword. The value of a is now", a)
myfun3()
print("After myfun3, the global a has been changed to", a)
Output
I am variable named a at the top of the program my value is 5
the value of a is 5
i am value of a inside myfun2 4
Outside myfun2, the global a is still 5
I used the global keyword. The value of a is now 3
After myfun3, the global a has been changed to 3

While global variables might seem convenient, overusing them can make debugging difficult because any part of your program can change their value unexpectedly. It is generally better to use Local variables and pass data between functions using arguments and return values.
Local Variables In Python Global Variables In Python Scope Of Variables In Python Difference Between Local And Global Variables In Python