Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Scope of Variable
3.
Types of Scope of variables
3.1.
Local scope of the variable
3.2.
Global Scope of variable
3.3.
Enclosing Scope of variable
3.4.
Built-in Scope of Variable
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Scope of Variable

Author Tashmit
0 upvote

Introduction

In programming, variables play the most crucial role in performing any operation. Variables are containers for storing different values. They can be pointers, references, objects to a class or just variable that is assigned some value. Python is known to be a statical language, i.e., in Python, we do not need to declare variables before allocating its value, in contrast to other languages like C++, C, or Java. There are various types of variables; we will discuss them in detail.

Source: Link

Recommended topics, Floor Division in Python, and Convert String to List Python

Scope of Variable

The scope of a variable is where a particular variable is declared and whether we can access it or not, whenever it is required. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Scope of variables

There are four types of the scope of a variable.

Source: Link

Local scope of the variable

Local Variables are those variables that are restricted to a single block in which they were created. They are narrowed to only within the function or class and cannot be accessed outside them.

Sample code

def fun():
    s = "I'm a proud ninja!"
    print(s)
fun()
print("\n",s)

 

Output:

As you can see, the variable s was created inside the function; therefore, the function was able to print it. But outside the function block, the variable s was not defined. The value of variable s is restricted in the function block only, and it cannot be accessed outside of the league.

Must Read, Divmod in Python

Global Scope of variable

As the name suggests, global variables are those variables that any program block can access. There is no restriction in the global variable and can be used either inside or outside a block. Generally, they are created at the beginning of the program.

Sample Code

s = "I'm a proud ninja!"
def fun():
    print("Inside function block: ",s)
fun()
print("Outside block: ",s)

 

Output:

We can create a global variable by mentioning the “global” keyword before the variable name. For example,

Sample code

def fun():
    global s
    s = "I'm a proud ninja!"
    print("Inside function block: ",s)
fun()
print("Outside block: ",s)

 

Output:

Here you can see that the string was accessed outside the function with the help of the global keyword. 

Recommended Article: Python Square Root

Enclosing Scope of variable

Enclosing scope is only applicable in Python. It is a remarkable scope that is applicable in nested functions. Let us understand this with the help of an example.

Sample code

def outerfun(num):
    number=num
    def innerFun():
        print(number)
    innerFun()
outerfun(500)

 

Output:

In this example, the innerun() function is called only when the outerfun() is called. Now, when we add some integer in the variable, the output will be

Sample code

def outerFun(num):
    number=num
    def innerFun():
        number = number + 45
        print(number)
    innerFun()
outerFun(500)

 

Output:

It gave an error. Now, this is when nonlocal variables come into the picture. Nonlocal variables are used in nested functions, which are not the local variable of the inner function. It is created inside the internal function by using the nonlocal keyword.

Sample code

def outerfun(num):
    number=num
    def innerfun():
        nonlocal number
        number = number + 45
        print(number)
    innerfun()
outerfun(500)

 

Output:

You can practice by yourself with the help of online python compiler.

Built-in Scope of Variable

The built-in scope is used when the variable is not found in local or global or enclosing Scope; then compiler starters look in the built-in directory. It is an enormous Scope that covers all the keywords, and it is effortless to call them without the need to define them.

Also Read,  leap year program in python

FAQs

  1. What is the Scope of the variable?
    The Scope of a variable is its existence in the program,i.e., the Scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.
     
  2. What do you mean by the visibility of a variable in Python?
    The concept of the Scope of variable rules how variables are looked up in the program that decides the visibility of a variable within the code.
     
  3. How many types of Scope of variables are there in Python?
    There are four types of the Scope of variable: Global, Local, Enclosing, Built-in scopes.
     
  4. How do you declare a variable in Python?
    There is no need to report a variable in Python; it is a statical language. The variable is created once it is assigned a value.

Key Takeaways

In this article, we have extensively discussed the Scope of variables, their types, and their implementation in Python. We hope that this blog has helped you enhance your knowledge regarding the scope of variables and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Lambda Function in Python
Next article
*Args and **Kwargs in Python
Live masterclass