Global Variables

Global variables are variables that are defined outside of any function and can be accessed and modified from any part of the program. In Python, you can use the global keyword to declare a variable as global within a function.

Accessing Global Variables

You can access a global variable from any part of your program, including functions. For example:

globalVar = 'I am a global variable'

def my_function():
    print(globalVar)

my_function()

Output:
I am a global variable

Modifying Global Variables

To modify a global variable from within a function, you need to use theglobal keyword to declare the variable as global. For example:

globalVar = 'I am a global variable'

def change_global_var():
    global globalVar
    globalVar = 'Changed global variable'

change_global_var()
print(globalVar)

Output:
Changed global variable

Conclusion

Global variables in Python allow you to store and access data from any part of your program. By using the global keyword, you can modify global variables within functions and use them to maintain state or share data across different parts of your program.



[ Footer Placeholder]