In this article, we will discuss an important concept, the scope of a variable in Python 3. In Python, there are four scopes possible for a given variable which refers to LGEB: Local, Enclosed, Global and Built-in Scopes.
- Local(L): Defined inside a function/class or enclosed within a nested loop or conditional construct.
- Enclosed(E): Defined inside enclosing functions (Nested functions)
- Global(G): Defined at the topmost level
- Built-in(B): Reserved Keywords in Python built-in functions/modules/classes
In simple terms, it is the order in which the namespaces are to be searched for scope resolution.
Note: Namespaces are named program regions used to limit the scope of variables inside the program.
Given below is the image which shows the hierarchal order in which the namespaces are to be searched for scope resolution: the first order is local, the second-order is enclosed, the third-order is global and the last order is Built-in.

Program to explain the local scope of a variable
Code:
var_scope = 'I am a variable with global scope'
def local_scope_func():
var_scope = "I am a variable with local scope"
print(var_scope)
local_scope_func()
Output:
I am a variable with local scope
Explanation:
In the above program, though we have defined two variables with the same name, one is at the topmost level and the other is inside the function. We can clearly see after executing our program that the variable inside the function is printed. It means the function searches the variable inside the local boundary first, If it is not found then only it will access the global variable.
Program to explain the local and global scope of a variable
Code:
var_scope = 'I am a variable with global scope'
def local_scope_func():
var_scope = "I am a variable with local scope"
print(var_scope)
local_scope_func()
print(var_scope)
Output:
I am a variable with local scope I am a variable with global scope
Explanation:
The output for the first line is clearly understood as discussed above. The print(var_scope) does not have access to the variable defined inside the function local_scope_fun() so it will access the global variable var_scope declared at the topmost level without any doubt.
Program to explain the local, enclosed and global scope of a variable
Code:
var_a = 'global variable'
def Function_at_level_one():
var_a = 'variable at level one'
def Function_at_level_two():
# var_a = 'variable at level two'
nonlocal var_a
print(var_a)
Function_at_level_two()
Function_at_level_one()
print(var_a)
Output:
variable at level one global variable
Explanation:
In this program, we have used keyword nonlocal, which is used to access the variable declared in the outer function.
Program to explain the local, enclosed, global and Built-in scope of a variable
Code:
from math import pi
# pi = 'global pi variable'
def outer():
# pi = 'outer pi variable'
def inner():
# pi = 'inner pi variable'
print(pi)
inner()
outer()
Output:
3.141592653589793
Explanation:
In this program, we have imported built-in variable pi from the math package. When we execute the function, it first searches the pi variable inside inner(), when it is not found it searches inside the outer() function, when it is not found, it searches the variable at the global level. When it is not found at global level, it uses the built-in pi variable to print