I am writing program as below for testing my knowledge about Python Scope of variable
def show(a,b):
def add():
a = a+b #Error at this line "UnboundLocalError: local variable 'a' referenced before assignment"
#I know we can use nonlocal a,b to avoid error
add()
print("a=",a)
x=4
y=2
show(x,y)
Then I tried same program with some little changes with x and y as list. Code is as shown below
def show(a,b):
def add():
a[0] = a[0]+b[0] #No Error at this line
add()
print("a=",a[0])
x=[4]
y=[2]
show(x,y)
And this code runs fine. I am not getting this strange behavior in python.