Difference Between Local and Global Variable in Python

Difference Between Local and Global Variable in Python

4 mins read285 Views 1 Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Jul 11, 2024 00:07 IST

In Python, variables play a pivotal role in storing data, but did you know they come in two distinct flavors: local and global? While a local variable is confined to the function where it’s defined, a global variable enjoys freedom, accessible throughout the entire program. But what implications does this have for your code? Why choose one over other? Explore the article and make informed coding decision.

2023_10_What-is-7.jpg

Table of Content

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Difference Between Local and Global Variables: Local Variables vs Global Variables

Parameter Local Variables Global Variables
Definition Defined inside a function or block. Defined outside of all functions or blocks.
Scope Accessible only within the function/block where it’s defined. Accessible throughout the entire program.
Lifetime Exists only during the function’s execution. Remains in memory for the duration of the program.
Accessibility Cannot be accessed outside its function. Can be accessed and modified by any function in the program.
Keyword for Modification No special keyword is required. Use the global keyword to modify it inside a function.
Memory Storage Stored in the stack. Stored in the data segment of memory.
Risk of Unintended Modification Low, as it’s confined to its function. Higher, as any function can modify it.

What is a Local Variable?

Local variables are variables that are defined inside a function. They can only be accessed within that function. These variables are created when the function is called and are destroyed when the function returns.
In simple terms, local variables can’t be modified outside the defined function, and its existence starts when the function is called and ends when the function completes its execution.

Let’s have an example to get a better understanding:


 
def my_function():
# This is a local variable
local_variable = 10
# Print the value of the local variable
print(local_variable)
# Call the function
my_function()
# Try to access the local variable outside of the function
# This will cause an error
print(local_variable)
Copy code

Output

2023_10_local-variable-in-python-1.jpg

The error comes because the variable local_varibale is only defined inside the function my_function. It can’t be accessed from outside of the function.

Note:

  • Local variables are useful for storing only needed data within a particular function.
  • It can store any type of object, including integers, floats, strings, lists, tuples, dictionaries, and even functions.
  • It can be reassigned values at any time.

Advantages and Disadvantages of Local Variable

Advantages of Local Variable Disadvantages of Local Variable
Limited to the function or block, reducing chances of accidental modification from other parts of the code. Cannot be accessed outside its function or block, which might require passing it as an argument if needed elsewhere.
Memory is efficiently managed as the variable is destroyed once the function completes its execution. Might lead to redundancy if similar local variables are declared in multiple functions.
Reduced risk of naming conflicts with variables from other functions or global variables. Ensure unique naming within the function to avoid conflicts with other local variables.

What is a Global Variable?

A global variable in Python is a variable that is defined outside of any function. It can be accessed from anywhere in the program. These variables help store the data that needs to be accessed throughout the program. But, generally, it is considered to avoid using global variables whenever possible. This is because global variables can make your code more difficult to read and maintain.

Let’s have an example to get a better understanding:


 
# This is a global variable
global_variable = "I am a global variable"
def display_global():
# Accessing the global variable inside this function
print(global_variable)
def modify_global():
global global_variable # Declare that we're using the global variable
global_variable = "I've been modified"
display_global()
modify_global()
display_global()
Copy code

Output

2023_10_global-variable.jpg

Explore the leading job-centric courses for life after 12th. Also, embark on specialized online degree programs for a fulfilling career journey.

Advantages and Disadvantages of Global Variable

Advantages of Global Variable Disadvantages of Global Variable
Accessible throughout the entire program, allowing multiple functions to share and modify the same data. It can lead to unintended modifications from different parts of the code due to its wide accessibility.
Exists for the duration of the program’s execution, ensuring consistent data availability. It can lead to inefficient memory usage if not managed properly, especially if the variable holds extensive data.
Provides a single source of truth for data that needs to be consistent across functions. Risk of data inconsistency if multiple functions modify the global variable without coordination

Key Difference Between Local Variables and Global Variables

  • Local variables are defined inside a function or block, whereas global variable is defined outside of all functions or blocks.
  • Local variables exist only during the function’s executions, while global variables remain in memory for the duration of the program.
  • Local variables can be accessed only within the function or block where it is defined, whereas global variables are accessed throughout the entire program. 
  • Global variables use the global keyword to modify it inside a function, whereas local variables do not require any special keyword.
  • Global variables are stored in the memory data segment, while local variables are stored in the stack.
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio

Comments

(1)

If you are suspecting that your husband is having an affair and he communicates with the person on his phone, you need to monitor his iPhone with the help of Tomcyberghost @ gmail com If he thinks that his phone cannot be tracked because it is an iPhone then he is wrong. TOMCYBERGHOST can give you a

Reply to Florence Jenny