I am doing the exercises in How to Think Like a Computer Scientist, and I couldn't get the answer to exercise number 11 in Chapter 11.
I am supposed to find why this code isn't working.
def swap(x,y):
print("before swap statement: x:", x, "y:", y)
(x,y) = (y,x)
print("after swap statement: x:", x, "y:", y)
a = ["This", "is", "fun"]
b = [2,3,4]
print ("before swap function call: a:", a, "b:", b)
swap(a,b)
print("after swap function call: a:", a, "b:",b)
the result of this code is
('before swap function call: a:', ['This', 'is', 'fun'], 'b:', [2, 3, 4])
('before swap statement: x:', ['This', 'is', 'fun'], 'y:', [2, 3, 4])
('after swap statement: x:', [2, 3, 4], 'y:', ['This', 'is', 'fun'])
('after swap function call: a:', ['This', 'is', 'fun'], 'b:', [2, 3, 4])
swap(a, b) takes a, b and does (a, b) = (b, a), so I'm curious as to why this doesn't work. Can someone give me an explanation or an answer?