How to Declare a Global Variable in Python
Global Variable in Python
In this tutorial, we will explore how to declare a global variable in Python, understand its scope, and learn how to modify it within functions.
Complete Python Course with Advance topics:-Click here
What is a Global Variable?
A variable that is available both inside and outside of functions and that is defined outside of any function is called a global variable. These variables have a global scope, meaning they can be used anywhere in the program unless overridden locally.
Let’s dive into the concept with an example.
Example 1:How to Define and Use a Global Variable
# Simple Python program to show how to define a global variable
# Defining a function
def func():
print("Inside the defined function the string is:", var)
# Printing the global variable within the function
# Declaring the global variable
var = "Declaring the Global Variable"
# Calling the function
func()
print("Outside the defined function the string is:", var)
# Printing the global variable outside the function
Output:
Inside the defined function the string is: Declaring the Global Variable
Outside the defined function the string is: Declaring the Global Variable
Accessing the Global Variable
The variable var is accessed both inside and outside of the function func() in the example above. It can be utilized across the program because it is declared in the global scope.
Overriding Global Variables with Local Variables
The global variable won’t be impacted if a variable of the same name is declared inside a function; instead, it will be handled as a local variable.
Example 2: Local and Global Variable Conflict
# Defining a function
def func():
var = "Global Variable" # Declared in local scope
print("The local scope variable is:", var)
# Printing the local variable
# Declaring the global variable
var = "I am learning Python"
func() # Calling the function
print("The global scope variable is:", var)
# Printing the global variable outside the function
Output:
The local scope variable is: Global Variable
The global scope variable is: I am learning Python
In this case, the global variable is unaffected by the local variable var inside the function.
Modifying Global Variables within a Function
If we try to modify a global variable inside a function without declaring it explicitly as global, Python will raise an error.
Example 3: Making an attempt to change a global variable without using a global
keyword
# Defining a function
def func():
var = var + " Global Variable"
print("Inside the defined function:", var)
# Trying to modify the global variable
# Declaring the global variable
var = "Python Tutorial"
func() # Calling the function
Output:
UnboundLocalError: local variable 'var' referenced before assignment
Using the global
Keyword
The global
keyword must be used in order to change a global variable inside of a function.
Example 4: How to Adjust a Global Variable Correctly
# Defining a function
def func():
global var # Declaring the variable as global
var = var + " Global Variable"
print("Inside the defined function:", var)
# Declaring the global variable
var = "Python Tutorial"
func() # Calling the function
print("Outside the function:", var)
# Checking if the global variable is modified
Output:
Inside the defined function: Python Tutorial Global Variable
Outside the function: Python Tutorial Global Variable
We tell Python that the variable inside the function refers to the globally defined variable by using the global keyword.
Combining Global and Local Variables
Let’s look at an example that demonstrates the interplay between global and local variables in multiple functions.
Example 5: Managing Local and Global Variables
# Declaring a global variable
var = 10
# Function without modifying the global variable
def func1():
print('Inside the first function:', var)
# Function with a local variable
def func2():
var = 15
print('Inside the second function:', var)
# Function modifying the global variable
def func3():
global var
var += 3
print("Inside the third function:", var)
print('Global value of variable:', var)
func1()
print('Global value of variable:', var)
func2()
print('Global value of variable:', var)
func3()
print('Global value of variable:', var)
Output:
Global value of variable: 10
Inside the first function: 10
Global value of variable: 10
Inside the second function: 15
Global value of variable: 10
Inside the third function: 13
Global value of variable: 13
Best Practices for Using Global Variables
- Use global variables sparingly to prevent unforeseen consequences.
- In order to avoid conflicts, give your variables meaningful names.
- Use constants (declared in uppercase) when variables should remain unchanged.
Download New Real Time Projects :-Click here
Complete Advance AI topics:-Â CLICK HERE
Conclusion
- Global variables allow data sharing across multiple functions.
- If you want to change a global variable inside a function, use the global keyword.
- Steer clear of using global variables excessively as this can complicate troubleshooting.
By understanding the proper use of global variables, you can write cleaner and more efficient Python programs.
Stay tuned to UpdateGadh for more Python tutorials and programming insights!
How to Declare a Global Variable in Python
Global Variable in Python
local variable in python
global variable in python class
local and global variables in python with example
python global keyword
use global variable in function, python
how to change global variable in function python
global function python
python global variable across files
online python compiler
lambda function in python
global variable in python example
global variable in python w3schools
global variable in python pdf
global variable in python geeksforgeeks
Post Comment