If you need to create a global variable, but are stuck in the local scope, you can use the global
keyword.
The global
keyword makes the variable global.
If you use the global
keyword, the variable belongs to the global scope:
def myfunc():
global x
x = 300
myfunc()
print(x)
Try it Yourself »Also, use the global
keyword if you want to make a change to a global variable inside a function.
To change the value of a global variable inside a function, refer to the variable by using the global
keyword:
x = 300
def myfunc():
global x
x = 200
myfunc()
print(x)
Try it Yourself »Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4