How to define global variables in Python?
In Python, the global keyword can be used to define global variables. These variables are defined outside of functions and can be accessed throughout the entire program.
Here is the method for defining global variables:
- Define global variables directly outside the function.
global_variable = 10
- worldwide
def my_function():
global global_variable
global_variable = 10
In both cases, global_variable remains a global variable, accessible from anywhere within the program.