How to Change Local Variables Using Global Keyword in Python


In this example, we will learn how to make local variables global using the global keyword in Python.

Source Code

def my_func():
    global var1     # make the variable global
    var1 = 5
    print(var1)

my_func()

print(var1)

Output:

5
5
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments