How to Change Local Variable Inside a Function in Python


In this example we will show how to change a local variable into a global variable inside a function using the global keyword in Python.

Source Code

var1 = 10        # this var1 is a global variable

def my_func():
    global var1     # make the variable global
    var1 = 15       # the value of the variable has changed
    print(var1)

my_func()

print(var1)

Output:

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