---
canonical_url: https://www.scaler.com/topics/difference between local and global variable in python/
title: What is the difference between local and global variable in python? | Scaler Topics
description: Learn the difference between local and global variable in python on Scaler Topics.
category: Python
author: Tarandeep Singh
---
:::section{.main}
## What is the Difference Between Local and Global Variable in Python?
Variables have a very important role in storing data and information. Before discussing the Difference Between Local and Global Variable in Python, let's see what the scope of variable really is.
### Scope of Variable
A variable's scope is basically the lifespan of that variable. It is the section of code to which a variable is alive. Depending on their scope, variables are divided into:
* Global variables
* Local variables
`Local variables` can only be accessed within the function or module in which they are defined, in contrast to global variables, which can be used throughout the entire program.
In Python, Global variable can be defined using `global` Keyword, also we can make changes to the variable in local context.
There are some key Difference Between Local and Global Variable in Python:
* `Global variables` are declared outside the functions whereas local variables are declared within the functions.
* `Local variables` are created when the function starts its execution and are lost when the function ends. Global variables, on the other hand, are created as execution of the program begins and are lost when the program is ended.
* In contrast to global variables, local variables do not offer data sharing.
* While global variables are kept in a fixed location selected by the compiler, local variables are kept on the stack.
* For local variables, parameter passing is necessary, but not for global variables.
* In Python, Global variables can be defined using `global` Keyword whereas local variables can be defined directly.
:::
<!--
What is the main difference between local and global variables in Python?
A) Global variables are declared within the functions whereas local variables are declared outside the functions.
B) Local variables are created when the program starts its execution and are lost when the program is ended. Global variables, on the other hand, are created as the function starts its execution and are lost when the function ends.
C) Local variables offer data sharing whereas global variables do not.
D) Local variables are kept in a fixed location selected by the compiler, whereas global variables are kept on the stack.
Answer: B) Local variables are created when the function starts its execution and are lost when the function ends. Global variables, on the other hand, are created as the program starts its execution and are lost when the program is ended.
-->
:::section{.main}
## Local Variable
Local variables are declared inside the function blocks. In Python, local variables can be declared at any location in the code block.
Only statements that are written inside a function can access local variables. They are secure in the way that no other function or variable of that program can access them.
Local variables are created during the execution of the function and are destroyed once the block has finished. As soon as the execution leaves the block in which a local variable is declared, it loses its content. It happens because local variables are always stored on the stack.
### Example:
Let's see a short example of how local variables can be defined.
```python::https://www.scaler.com/topics/python/online-python-compiler/?content_slug=1011e655bc90b9ce7bc6
def fun():
a = 10
print(a)
fun()
```
### Output:
```plaintext
10
```
:::
:::section{.main}
## Global Variable
`Global variables` are the types of variables that are declared outside of every function of the program. The global variable, in contrast to local variables, is accessible by all functions in a program. Global variables are not very reliable because any function in the program can alter their value.
They continue to exist until the entire program has been ended. Global variables hold onto their values throughout the program execution. The compiler-determined fixed region of memory where they are stored is the cause.
A global variable is useful when many functions are using the same set of data. Utilizing a lot of global variables could be challenging because they could undergo unwanted changes in value.
### Example:
Let's see a short example of how global variables can be defined. Here, we are simply accessing the global variable using the global keyword.
```python::https://www.scaler.com/topics/python/online-python-compiler/?content_slug=f17351d4770c48c45c30
a = 10
def fun():
global a
print(a)
fun()
```
### Output:
```plaintext
10
```
:::
<!--
Which of the following statements about global variables is true?
A) Global variables are only accessible by the function in which they are declared.
B) Global variables are not reliable because they cannot hold onto their values.
C) Global variables are stored in a compiler-determined fixed region of memory.
D) Utilizing a lot of global variables can be challenging because they could undergo unwanted changes in value.
Answer: C
-->
:::section{.main}
## Local Variable Vs. Global Variables
Lets see the tabular difference Between Local and Global Variable in Python
| Comparision Basis | Global Variable | Local Variable |
|:---------------------------:|:------------------------------------------------------------------------------------------------------:|:------------------------------------------------------------------------------------------------------:|
| Definition | Global variables are declared outside the functions | Local variables are declared within the functions |
| Lifetime | Global variables are created as execution of the program begins and are lost when the program is ended | Local variables are created when the function starts its execution and are lost when the function ends |
| Data Sharing | Global Variables Offers Data Sharing | Local Variables doesn't offers Data Sharing |
| Scope | Accessible throughout the code | Accessible inside the function |
| Storage | Global variables are kept in a fixed location selected by the compiler | Local variables are kept on the stack |
| Parameter Passing | For global variables, parameter passing is not necessary | For local variables, parameter passing is necessary |
| Changes in a Variable Value | Changes in a global variable is reflected throughout the code | Changes in a local variable doesn't affect other functions of the program |
| Naming Restrictions | Global variables should be uniquely named i.e. many variables with the same name cannot exist. | Different variables with the same name can be declared in different functions. |
| Memory | The global variable takes zero by default if it is not initialised. | If the local variable is not initialized, it defaults to a garbage value. |
:::
<!--
Which of the following is a difference between Local and Global Variables in Python?
A) Global variables are declared within the functions while local variables are declared outside the functions.
B) Global variables are created when the function starts its execution and are lost when the function ends while local variables are created as execution of the program begins and are lost when the program is ended.
C) Global variables offer data sharing while local variables do not offer data sharing.
D) Local variables are accessible throughout the code while global variables are accessible inside the function.
Answer: B
-->
:::section{.main}
## Examples to Understand Differences Between Local and Global Variable
Let's see an example where we can more clearly understand the Difference Between Local and Global Variable in Python.
### Example of Local Variable:
In this example, we are making a local variable inside the function and trying to access that local variable outside the function.
```python::https://www.scaler.com/topics/python/online-python-compiler/?content_slug=acd317b9e7346152415d
def func():
a = 10
print(a)
func()
print(a)
```
### Output:
We can see that an error pops up while accessing the variable outside the function, as the scope of that variable is only up to that function.
```plaintext
[NonZeroExitCode] Your code encountered a runtime error
Traceback (most recent call last):
File "main.py", line 5, in <module>
print(a)
NameError: name 'a' is not defined
```
### Example of Global Variable:
In this example, we are making a global variable and are trying to access that global variable inside and outside the function.
```python::https://www.scaler.com/topics/python/online-python-compiler/?content_slug=1312b236e2bcf08f0fd7
a=10
def func():
global a
a+=10
print("Value inside the function:",a)
func()
print("Value Outside the function:",a)
```
### Output:
We can see that we can access the global variable both outside the function as well as inside the function.
```plaintext
Value inside the function: 20
Value Outside the function: 20
```
:::
:::section{.main}
## Advantages and Disadvantages of Global and Local variables
We have discussed Difference Between Local and Global Variable in Python, now lets discuss about their advantages and disadvantages:
### Advantages of Local Variables
* The main advantage of a local variable is that the data is not accidentally changed. Variable declared inside a function makes use of that variable while avoiding adverse side effects.
* When the block containing the variable is executed, a local variable only uses memory for a brief period of time.
### Advantages of Global Variables
* When dealing with multiple functions in the program that are manipulating the same data, global variables are very helpful.
* The use of a global variable makes it simpler to make changes that are needed to be made throughout the entire programme.
* For global variables, access is available from anywhere or via any program function at random.
### Disadvantages of Local Variables
* The local variable's scope is restricted.
* Local Variable prevents sharing of data.
* Since local variables are created and destroyed with each entry and exit from the block, they cannot save the data in-between the function calls.
### Disadvantages of Global Variables
* The use of numerous global variables may lead to the creation of programming errors.
* The main issue it creates is the accidental changes in data that arise as a result of the program's multiple global variables.
* It might also necessitate performing code refactoring, a time-consuming process that involves reorganizing the entire program's code.
:::
:::section{.main}
## Which One is More Useful?
In Python programming, both local and global variables are crucial when writing programs. However, many global variables could take up a lot of memory. It is getting harder to spot an undesireable change in global variables. As a result, it is wise to avoid declaring pointless global variables and using local variables for passing and manipulating data. It is generally good practice to make use of local variables in Python.
:::
:::section{.summary}
## Conclusion
* A reserved memory address for storing values is called a variable. A variable in a Python programme provides the computer with data to be processed.
* Variables in Python have different scopes, representing their lifespan and access within the code.
* Variables are divided into:
* Global variables
* Local variables
* Local variables are created when a function starts and destroyed when it ends, while global variables are created when the program begins and lost when it ends.
* Local variables can only be accessed within the function or module in which they are defined, in contrast to Global variables, which can be used throughout the entire program.
* Local variables are declared inside the function blocks whereas Global variables are the type of variables that are declared outside every function of the program.
* Local variables are stored on the stack, with their memory allocated and deallocated as functions are called and return. Global variables are stored in a fixed memory location determined by the compiler.
* Local variables do not offer data sharing between functions, while global variables are accessible by multiple functions, enabling data sharing.
* It is a good practice to use local variables to avoid unnecessary memory consumption and unintended changes in global variables.
* When developing a programme in any language, both local and global variables are equally crucial. However, a sizable portion of the global variables could require a lot of RAM.
:::