In this article, we will learn about the solution and the number of local variables present in a function.
Detect the number of local variables declared in a function
Example:-
Input : a = 1
b = 2.1
str = ‘local Variables in Function’
Output : 3
Code 1): python program to detect the number of local variables declared in a function

# Code 1):-
def scope():
a = 45
b = 87
str_ = ‘The local or global’
# main
print(“Number of local varibales available:”,scope.__code__.co_nlocals)
OUTPUT:- Number of local varibales available: 3
How to write the program to detect the number of local variables declared in the function
- first, we take a function with the name of the scope
- after we take some variables to check the local or global in the functions
- Use (.__code__.co_nlocals) built-in function to detect the number of locals variables in the function
- The answer is 3
code 2):-another method to detect the number of local variables declared in python

def scope():
a,b,c = 45,455,”hello python”
# main
print(“Number of local varibales available:”,scope.__code__.co_nlocals)
OUTPUT:- Number of local variables available: 3
In this program, we use the same method but we take variables in the same line or in serial
Out is same 3.