58 Program to demonstrate global variable in Python

Program:

#modifying global variables from within the function.



x=10

def display():

    #IF I want to modify global variable 'x' value

    global x  #forward declaration for modifying global variable

    x=40   #here modifying global variable 'x' value from 10 to 40

    print(x)

   

def display2():

    print(x)





display2()

display()

display2()


Output:

Program to demonstrate global variable in Python

Previous
Next Post »