- Get link
- X
- Other Apps
In Python, a global variable is a variable that is declared outside of any function and can be accessed by any function in the same module or file. Global variables can be useful for storing values that are needed by multiple functions within the same module or file. In Python, declaring a variable outside of any function is all that's needed to create a global variable: ``` # defining a global variable my_global_var = "Hello World" # Creating a function that utilizes the global variable: def print_global_var(): print(my_global_var) # calling the function print_global_var() # output: "Hello World" ``` In the example above, `my_global_var` is a global variable that can be accessed by the function `print_global_var()` because it is defined outside of any function. It is also possible to modify the value of a global variable from within a function using the `global` keyword: ``` # defining a global variable my_global_var = "Hello World" # defining a ...