Global vs Local Variables in Python
In Python, when we write programs, we use variables to store data. Depending on where a variable is created, it can be a global variable or a local variable.
Global variables are declared outside any function. They can be accessed and used in any part of the program, including inside functions.
Local variables are declared inside a function. They only exist while the function is running, and cannot be used outside that function.
What is a Local Variable?
A local variable is a variable that is declared inside a function. It only works inside that function. When the function finishes, the local variable is deleted from memory.
Example:
def show():
x = 5 # local variable
print("Inside function, x =", x)
show()
# print(x) # This will give an error: NameError: name 'x' is not defined
Output:
Inside function, x = 5
Explanation:
- When we call
show(), it prints:Inside function, x = 5 - If we try
print(x)outside the function, it gives an error becausexdoes not exist outsideshow(). - This proves that local variables cannot be used outside their function.
What is a Global Variable?
A global variable is declared outside all functions. It is available everywhere in the program, both inside and outside functions.
Example:
x = 10 # global variable
def display():
print("Inside function, x =", x)
display()
print("Outside function, x =", x)
Output:
Inside function, x = 10
Outside function, x = 10
Explanation:
xis declared outside any function, so it is a global variable.- When we call
display(), it prints:Inside function, x = 10 - When we print
xoutside the function, it prints:Outside function, x = 10 - This shows that global variables can be accessed from anywhere in the program.
Difference Between Local and Global Variables
| Local Variable | Global Variable |
|---|---|
| Declared inside a function. | Declared outside all functions. |
| Can be used only inside that function. | Can be used in any function of the program. |
| Created when the function starts and deleted when it ends. | Exists till the whole program runs. |
| Does not change the global variable with the same name. | Can be used in functions if there is no local variable with the same name. |
| Used for small tasks inside a function. | Used to keep data that many functions need. |
| Memory is used for a short time. | Memory is used for the entire program time. |
| Mainly used for temporary calculations inside functions. | Used to store data that is needed by many functions. |
Note:
If we create a local variable with the same name as a global variable, inside the function, the local variable is used.
Example: Local variable with same name as Global variable
x = 15 # global variable
def demo():
x = 5 # local variable
print("Inside function, x =", x)
demo()
print("Outside function, x =", x)
Output:
Inside function, x = 5
Outside function, x = 15
Explanation:
- There is a global variable
x = 15. - Inside the function
demo(), a local variablex = 5is created. - When we call
demo(), it prints:Inside function, x = 5 - When we print
xoutside, it prints:Outside function, x = 15 - This shows that the local variable does not change the global variable.
Summary:
- Local variables are created inside functions and cannot be used outside.
- Global variables are created outside functions and can be used anywhere.
- If we create a local variable with the same name as a global variable, inside the function, the local variable is used.
- This helps keep data separate and prevents unwanted changes.