Python program to demonstrate variables scope

Here, we are executing a Python program, that will show the standards about the variable degrees. In the model,

we are utilizing the worldwide variable and area variable, getting to, changing their values inside their degrees.

A worldwide variable can be gotten to anyplace in the program, it’s a degree is worldwide to the program,

while a nearby factor can be gotten to inside a similar square where the variable is announced

on the off chance that we attempt to get to a neighbourhood variable outside of the extension – it will give a mistake.

Program

# Python code to demonstrate example 
# of variable scopes

# global variable
a = 100

# defining a function to test scopes
def func():
    # local variable
    b = 200

    # printing the value of global variable (a)
    # and, local variable (b)
    print("a: ", a, "b: ", b)
    
# main code
if __name__ == '__main__':
    # local variable of main
    c = 200
    
    # printing values of a, b and c
    print("a: ", a) #global 
    # print("a: ", b) #local of text *** will give an error
    print("c: ", c) # local to main
    
    # calling the function
    func()
    
    # updating the value of global variable 'a'
    a = a+10
    
    # printing 'a' again
    print("a: ", a) #global

Output

Python program to demonstrate variables scope

Leave a Comment

error: Alert: Content is protected!!