Scope of The Variables in Python

Pallavi Dhotre

6 months ago

Scope of the variables in Python | insideAIML
Table of content
Introduction
Function and Scoping in python
Scope of variables in python
  • Local Scope in python
  • Global Scope in python
  • Global keyword in python
  • Nonlocal Keyword in python
  • Example
Summary

Introduction 

          In every programming language, we use variables. Variables store the value and are usually known as the location or space in the main memory. Sometimes we use variables within the function, or sometimes, we use outside the function. The scope of variables in python completely depends on where we declare the variables. So before we start with python scoping, let’s understand - What is a function? and Function and scoping in python. 

Function and Scoping in python

          The  function  is  a  set  of  executable  statements  or  instructions. Functions  are  also known as subprograms. In some cases, variables have the same name within the function and outside the function. But they hold different values. Variables that are declared inside the function can be accessed by that function only. We cannot access those variables outside the function. And the variables which are declared outside the function can be accessed inside as well as outside the function. Let's understand the function and scoping in python with the help of an example:
def myFunction(): #function Declaration
    a=10          # variable declaration inside the function
    print(a)      
myFunction() #Function Calling

In the above example, we have declared the variable inside the function. So the variable will be accessed only within the function.
a=10 # variable declaration outside the function
def myFunction(): #function Declaration
    print(a)  #accessing variable inside the function 
myFunction() #Function Calling
print(a)  #accessing variable outside the function
in the above example, the variable is declared outside the function. And we can access this variable inside the function as well as outside the function.
Now, let’s understand the scope of variables in python in detail.

Scope of Variables in Python

          The scope of variables in python completely depends on where we declare the variables and from where we can access them. As seen earlier in functions, if we declare the variables inside the function, we can access that variable inside the function only. That means the python variable scope is limited to function. Usually, this concept is known as the local scope of variables in python. Similarly, if we declare the variable outside the function, we can access it anywhere in the program. That means we can access it in the function also. This concept is known as the global scope in python. To elaborate further, let’s look at a brief example of python scoping: 
Let’s consider language as a variable and Country as a function. In India, Hindi is the most popularly known language. So the scope of the language Hindi is limited to India only. Now, consider the language English. English is widely recognized across most nations. Scope of language English is not limited to any specific country. It is used in India and outside India. So in this example, the scope of variable Hindi is local and the scope of variable English is global. So, this was all about the scope of variables in general. Now, let’s deep dive into the python scoping to understand its subdomains: 

Local Scope in python

          If we declare a variable inside the function, then the variable is known as a local variable or known to have a local scope. That means the variable can be accessed within the function only. For example,
#Local scope of variables in python
def myFunction(): #function Declaration
    a=100         # variable declaration inside the function
    print(a)      
myFunction() #Function Calling
In the above example, we have used a local scope of variables in python. If we try to access the variable outside the function in the following way:
def myFunction(): #function Declaration
    a=10          # variable declaration inside the function
    print(a)
myFunction() #Function Calling
print(a) #accessing local variable outside the function
Unfortunately, this code will first display value 100 and then throw a NameError: Name ‘a’ is not defined.

Global Scope in python

          If we declare a variable outside the function, then it can be accessed all over the program and inside the function also. These types of variables are known as global variables or known to have a global scope in python. For example,
#global scope of variables in python
a=35 #declaring variable outside the function
def myFunction(): #function Declaration
    print(a) #accessing variable inside the function
myFunction() #Function Calling
print(a) #accessing variable outside the function
In the above example, we have used a global scope of variables in python. If we access the variable inside the function then also it will print the same value. 
So, the above code will print:
35  
35

Global keyword in python

          There is another method of using the variable from the local scope as a global. Remember, global scope and global keyword in python are two different things. In the global scope, we declare variables outside the function. While using the global keyword in python we declare the variable inside the function preceded with the global keyword. In this case, we can access the variable declared within the function from outside the function. Let’s understand the concept of the global keyword in python with the help of an example:
def myFunction(): #function Declaration
    global a #using the global keyword
    a=500 #variable declared within the function
myFunction() #Function Calling
print(a) #accessing variable outside the function

In the example given above, first, we have used a global keyword before a variable name inside the function. Then we have assigned a value to the variable inside the function. And then, we are accessing the variable outside the function. The output of the program will be 500.

Nonlocal Keyword in python

          The  nonlocal  keyword  in  python  is used to  work  with  the  variable declared within the nested function. Use keyword nonlocal for declaring that the variable used within the inner function is not local. see the example given below for a better understanding of the nonlocal keyword in python
def f1(): #outer function
 x = "Welcome" #local variable in outer function
 def f2(): #inner function
   nonlocal x #declaring variable as nonlocal
   x = "hello"
 f2() #calling inner function
 return x
print(f1()) #calling and printing outer function
In the given code there are two functions. We have declared variable x as nonlocal in the inner function. So the output of the program using the nonlocal keyword in python will be hello.
Example:
#python variable scope
a=300 #global variable
def myFunction(): #function Declaration
    b=200 #Local variable
    global c #using the global keyword
    c=500 #variable declared within the function
    print("This is local variable",b)   #accessing local variable inside the function
    print("This is variable declared using global keyword",c)   #accessing variable declared using global keyword inside the function
myFunction() #Function Calling
print("This is global variable",a)   #accessing variable outside the function
print("This is variable declared using global keyword",c)   #accessing variable declared using global keyword outside the function
This is local variable 200
This is variable declared using global keyword 500
This is global variable 300
This is variable declared using global keyword 500

Summary

          In this article, we have learned about the functions and scoping in python, the scope of variables in Python, local scope, global scope, global keyword in python, and nonlocal in python. The scope of the variables can be defined by observing where the variables are declared. Variables declared within the function are called Local Variables and the variables declared outside the function are called Global Variables. Local Variables cannot be accessed outside the function but Global Variables can be accessed within the function. Also, we can use a Global Keyword to access the local variables outside the function. If the variable is declared within the nested function then we can use keyword nonlocal in python. So, this is all about the scope of variables in python.
       
We hope you enjoyed the article. If you have any related queries related to the scope of variables in python, feel free to ask in the comment section below.
      
Liked the Blog, then Share it with your friends and colleagues to make this AI community stronger. 
To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our blog page
Keep Learning. Keep Growing. 

Submit Review