Global and Local Variables in Python - GeeksforGeeks

PHOTO EMBED

Tue Apr 04 2023 03:31:46 GMT+0000 (Coordinated Universal Time)

Saved by @tofufu #python

a = 1
 
# Uses global because there is no local 'a'
def f():
    print('Inside f() : ', a)
 
# Variable 'a' is redefined as a local
def g():
    a = 2
    print('Inside g() : ', a)
 
# Uses global keyword to modify global 'a'
def h():
    global a
    a = 3
    print('Inside h() : ', a)
 
 
# Global scope
print('global : ', a)
f()
print('global : ', a)
g()
print('global : ', a)
h()
print('global : ', a)
content_copyCOPY

https://www.geeksforgeeks.org/global-local-variables-python/