Scopes in Python

Scopes in Python

Β·

3 min read

Namaste Coders πŸ‘©β€πŸ’»

Before starting have a glance at the below article to know about functions in Python πŸ‘‡

Built-in Functions User Defined Functions Lambda Functions

Scope of the variables 🧐

Scope of a variable defines its accessibility according to locations. A variable may not be accessible at all locations. It completely depends on where we have declared the variables.

Variables have been classified into two types in Python :

  • Global variables
  • Local variables

Global variables

Variables that can be accessible at all locations and are defined outside any block such as functions,loops, etc . are known as global variables. These are accessible throughout the program.

Check out the example πŸ‘‡

# global variable
total=0

def sum(num1,num2):
    total=num1+num2
    print("Total is :",total)
    return total
num1=5
num2=10
print("Sum is :",sum(num1,num2))

#Output:
#Total is : 15
#Sum is : 15

Local variables

Variables which are declared inside a particular block (functions,loop,if-else) and cannot be accessed from outside are known as local variables. They have scope limited and confines to that block only.

Look at the following example πŸ‘‡

# global variable
total=0

def sum(num1,num2):
    # local variable
    add=num1+num2
    print("Sum is :",add)

num1=4
num2=3
sum(num1,num2)
print("Total is: ",total)


#Output:
#Sum is : 7
#Total is:  0

If we try to access local variable outside its scope:

# global variable
total=0

def sum(num1,num2):
    # local variable
    add=num1+num2


num1=4
num2=3
sum(num1,num2)
print("Total is: ",total)

# It will give an error if we try to access local variable outside
print("add variable outside: ", add)

#Total is: 0
#NameError: name 'add' is not defined

Global Keyword:

Look following code πŸ‘‡

#global variable 
num=10

def func():
    # local variable
    num=2
    print("Inside function:",num)

func()
print("Outside function: ",num)

#Output:
#Inside function: 2
#Outside function:  10

If you want global **variable **num used inside the func function rather than creating local variable. So to achieve that we use global keyword.

#global variable 
num=10

def func():
    # global keyword used to specify it is same global variable used
    global num
    num=2
    print("Inside function:",num)

func()
print("Outside function: ",num)


#Output
#Inside function: 2
#Outside function:  2

nonlocal keyword

This is another keyword used while learning the scope of variables. It plays an important role while we are working with nested functions.

num=10
def outer():
    # local variable of outer function
    num=2
    print("Inside outer function:",num)
    def inner():
        #using local variable of parent function
        nonlocal num
        num=100
    #calling inner function
    inner()
    #value of num afterwards
    print("value of num after inner func:",num)

outer()
# value of global variable remains same
print("Outside function: ",num)


#Output:
#Inside outer function: 2
#value of num after inner func: 100
#Outside function:  10

The End

I hope you enjoyed the article and had a good learning experience.

Follow for more articles and keep sharingπŸ‘©

Keep coding

Python blogs

Linkedin

hashnode blogs

Did you find this article valuable?

Support akshita garg by becoming a sponsor. Any amount is appreciated!

Β