Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this tutorial, you'll learn about global variables and the global
keyword in Python. Global variables are variables that are accessible from any part of your code, unlike local variables, which are only accessible within the function or block of code where they are defined.
Global variables are defined outside of any function or block of code. They can be accessed and modified by any function in the program.
# Define a global variable greeting = "Hello, World!" def print_greeting(): print(greeting) # Call the function print_greeting() # Output: Hello, World!
In this example, we define a global variable greeting
and use it inside the print_greeting
function.
Local variables are defined within a function or block of code and are only accessible within that scope.
def print_greeting(): # Define a local variable greeting = "Hello, World!" print(greeting) # Call the function print_greeting() # Output: Hello, World!
In this example, we define a local variable greeting
inside the print_greeting
function.
global
keyword:If you want to access and modify a global variable from within a function, you need to use the global
keyword before the variable name.
# Define a global variable greeting = "Hello, World!" def change_greeting(): global greeting # Use the global variable greeting = "Hello, Python!" # Call the function change_greeting() print(greeting) # Output: Hello, Python!
In this example, we use the global
keyword to access and modify the global variable greeting
from within the change_greeting
function.
When you import a module, its global variables become accessible using the module's name as a prefix.
# my_module.py greeting = "Hello, World!" def print_greeting(): print(greeting) # main.py import my_module # Access the global variable from the module print(my_module.greeting) # Output: Hello, World! # Call the function from the module my_module.print_greeting() # Output: Hello, World!
In this example, we define a global variable greeting
and a function print_greeting
in my_module.py
. We then import my_module
in main.py
and access its global variable and function.
In this tutorial, you learned about global variables and the global
keyword in Python. Global variables are useful for sharing data between different parts of your program, but you should use them judiciously to avoid making your code harder to understand and maintain.
Using global variables in Python:
global_variable = 10 def print_global_variable(): print("Global Variable:", global_variable) print_global_variable()
Python global keyword example:
global
keyword is used to indicate that a variable is a global variable, allowing modification within functions.global_variable = 10 def modify_global_variable(): global global_variable global_variable += 5 modify_global_variable() print("Modified Global Variable:", global_variable)
Global variables in nested functions Python:
global
keyword.global_variable = 10 def outer_function(): global_variable = 5 # This creates a new local variable def inner_function(): nonlocal global_variable global_variable += 2 inner_function() print("Modified Global Variable in Nested Function:", global_variable) outer_function()