Local Variables
A variable declared inside the function’s body or in the local scope is known as local variable.
Example:
def abc():
    y="local"
    print(y)
abc()
Output:
local
Example:2
Example:2
def abc():
     a=100
     print("a in abc() function :", a)
def pqr():
     a=200
     print("a in pqr() function :", a)
     
abc()
pqr()
Output:
a in abc() function : 100
a in pqr() function : 200
>>>
In the above example:
1. variable “a” is a local variable in function abc() so its value is displayed as 100, And
2. variable “a” is a local variable in function pqr() so its value is displayed as 200
Example:3
def abc():
    y="local"
    print(y)
abc()
print(y)
Output:
When we run the code, the will output be (Error):
NameError: name ‘y’ is not defined
The output shows an error, because we are trying to access a local variable y in a global scope whereas the local variable only works inside abc() or local scope.
Example:4
#function definition
def abc():
     a=100
     print("a in abc() function :", a)
#function calling 
abc()
print("a outside abc() function :", a)
Output:
a in abc() function : 100
NameError: name ‘a’ is not defined
>>>
The output shows an error, because we are trying to access a local variable “a” in a global scope whereas the local variable only works inside abc() or local scope.




