Friday, August 9, 2013

Python: Why is this the output when executed?

Python: Why is this the output when executed?

Can someone tell me why, after I run the code below, these are the values
the variables refer to now:
x = [1, 2, 3, 5]
x1 = [1, 2, 3, 5]
x2 = [1, 2, 3]
y = [1, 2, 3]
y1 = [1, 2, 3]
y2 = [1, 2, 3]
Why isn't y = [1, 2, 3, 5] ?
And why isn't x1 = [1, 2, 3]?
Because, following this logic:
a = 1
b = a
a = 3
b is still equal to 1, not 3.
Code is below. Thanks for your help.
def my_function(x, y):
x.append(5)
y = y + [5]
x = [1, 2, 3]
x1 = x
x2 = x[:]
y = [1, 2, 3]
y1 = y
y2 = y[:]
my_function(x, y)

No comments:

Post a Comment