All Courses

Scope of The Variables in Python

Pallavi Dhotre

a year ago

Scope of variables in python thumbnails 
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.  

Frequently Asked Questions:

Q1: What is a built-in scope in python?

Answer 1: A built-in scope in Python refers to the predefined variables and functions that are available to use without importing any additional modules. Examples of built-in functions in Python include print(), len(), and range(), while examples of built-in variables include True, False, and None. Built-in scopes are global and are available to all parts of the program.

Q2: Does python have a scope?

Answer 2: Yes, Python has a scope system known as the LEGB (Local, Enclosing, Global, and Built-in) rule. Variables defined in a specific block of code (such as a function or class) are only accessible within that block and are said to have local scope. Variables defined in an enclosing block (such as a containing function) have an enclosing scope and can be accessed by inner blocks. Variables defined at the top level of a module or script have global scope and can be accessed by all blocks in the module or script. Finally, built-in variables and functions have a built-in scope and can be accessed from anywhere.

Q3: What are the local and global scopes in python?

Answer 3: 
In Python, a variable's scope refers to the region of the code where it can be accessed or modified.
A local scope is one where a variable is defined within a function or class method, and can only be accessed or modified within that function or method. For example, a variable defined inside a for loop can only be accessed within the loop.
A global scope is one where a variable is defined outside of any function or class method and can be accessed or modified from anywhere in the code. For example, a variable defined at the top level of a script can be accessed from any function or method defined in that script.
It is worth noting that if you want to modify the global variable inside the function you should use a global keyword before the variable name.
      

Submit Review