Q:

python access global variable

global variable
variable = 'whatever'
10
global var1
var1 = 'whatever'
21
globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1
4
global n
n = 'whatever'
7
# Python program showing no need to 
# use global keyword for accessing 
# a global value 
  
# global variable 
a = 15
b = 10
  
# function to perform addition 
def add(): 
    c = a + b 
    print(c) 
  
# calling a function 
add() 
0

New to Communities?

Join the community