In this tutorial, you will learn how to change a variable outside of a function in Python.
You can use the variable as global in Python which allows you to change or modify the variable values in any other function in Python.
Using the Global Variable Method To Change A Variable Outside Of A Function
Let us see in the below example code how you can use the global variable method to change variable value outside of a function in Python.
#Initializing the Global Variable outSide = 10 def changeInFunc(): global outSide outSide = 20 #Calling the Fuction to Change #Variable value to 20 changeInFunc() print("The previous value is {}".format(outSide)) #Changing the Variable to prev outSide = 10 print("The Current Value of Variable is {}".format(outSide))
Output:
The previous value is 20
The Current Value of Variable is 10
As you can see in the above code, I was able to change the variable value outside of a function. Also, in the above example, you can see that you can access and change the variable inside the function if you want to do that as well.

Wrap Up
I hope you understood how to change variables outside of any function. All you are required to do is change the variable as global in python and you can access and change the variable from anywhere you want.
Let me know in the comment section if you have any better solution than the one provided above. I will be happy to add it here.
If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read: