Post

Answer: a) [1, 2, 3]
The .copy() saves the day!
› y = x.copy() creates a NEW list in memory
› x.append(4) only modifies x's list
› y remains unchanged: [1, 2, 3]
For beginners: Lists are boxes, variables are stickers. y = x puts another sticker on the same box. y = x.copy() creates a new box!
English

@PythonPr Python students seeing .copy() for the first time be like: ‘Wait… why didn’t it change too?’
English

@PythonPr A. Since y is an independent copy of subsistent changes on x so it’s [1,2,3]
English

@PythonPr Too easy
Let's make it weirder:
x = [[1, 2, 3]]
y = x.copy()
y[0].append(4)
print(x)
What will the output be?
English






















