def foo(a, b):
a = 1
b[0] = 2
p, q = 100, [200, 300]
foo(p, q)
print(p, q)
>>> 100 [2, 300]
My question is that, why q changes, but p does not? Thank you!
def foo(a, b):
a = 1
b[0] = 2
p, q = 100, [200, 300]
foo(p, q)
print(p, q)
>>> 100 [2, 300]
My question is that, why q changes, but p does not? Thank you!
q is a list therefore is "passed by reference" to the function. p is passed by value.