In this tutorial, we will dive into the world of Python Global, exploring its usage and various examples to help you understand the concept.
What is Python Global?
In Python, global is a keyword that is used to define a variable inside a function as a global variable. A global variable is a variable that can be accessed by any function or module in the program. It is not confined to a specific function or module.
By default, a variable defined inside a function is considered a local variable. Local variables are confined to the function in which they are defined and cannot be accessed outside the function. However, if we want to access the variable outside the function or make it available to other functions, we can define it as a global variable using the “global” keyword.
Usage of Python Global
The “global” keyword is used to define a variable as a global variable inside a function. The syntax for using the global keyword is as follows:
global variable_name
Here, “variable_name” is the name of the variable that we want to define as a global variable. We can also assign a value to the global variable inside the function.
Examples of Python Global
Let’s look at some examples to understand the usage of Python Global.
Example 1: Defining a Global Variable
# Defining a global variable
def my_function():
global x
x = 10
my_function()
print(x)
Output:
10
In this example, we define a global variable “x” inside the function “my_function()” by using the “global” keyword. We assign a value of 10 to the global variable “x” inside the function. Then, we call the function and print the value of the global variable “x”. Since “x” is a global variable, it can be accessed outside the function.
Example 2: Accessing a Global Variable Inside a Function
# Accessing a global variable inside a function
x = 10
def my_function():
print(x)
my_function()
Output:
10
In this example, we define a global variable “x” outside the function “my_function()”. We then define the function “my_function()” and print the value of the global variable “x” inside the function. Since “x” is a global variable, it can be accessed inside the function.
Example 3: Modifying a Global Variable Inside a Function
# Modifying a global variable inside a function
x = 10
def my_function():
global x
x = 20
my_function()
print(x)
Output:
20
In this example, we define a global variable “x” outside the function “my_function()”. We then define the function “my_function()” and modify the value of the global variable “x” inside the function using the “global” keyword. After calling the function, we print the value of the global variable “x”. Since “x” is a global variable, it can be modified inside the function.
Example 4: Using a Global Variable in Different Modules
# Using a global variable in different modules
# module1.py
x = 10
# module2.py
import module1
def my_function():
print(module1.x)
my_function()
Output:
10
In this example, we define a global variable “x” in the module “module1”. We then import the module “module1” in another module “module2”. We define the function “my_function()” in “module2” and print the value of the global variable “x” from “module1” inside the function. Since “x” is a global variable, it can be accessed by other modules.
Example 5: Using a Global Variable as a Counter
# Using a global variable as a counter
counter = 0
def increment_counter():
global counter
counter += 1
increment_counter()
increment_counter()
increment_counter()
print(counter)
Output:
3
In this example, we define a global variable “counter” outside the function “increment_counter()”. We then define the function “increment_counter()” and use the “global” keyword to define “counter” as a global variable. We call the function three times to increment the value of “counter” by 1 each time. Then, we print the final value of “counter”. Since “counter” is a global variable, it can be used as a counter.
Conclusion
In this tutorial, we have learned about the Python Global keyword, its usage, and various examples to help you understand the concept. We have seen:
- How to define a global variable
- How to access a global variable inside a function
- How to modify a global variable inside a function
- How to use a global variable in different modules
- How to use a global variable as a counter
We hope this tutorial helps you understand the concept of Python Global better.