Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
33
24
169
16.6K
Sergei Kotov
Sergei Kotov@kotov_dev·
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
3
0
33
773
Everton Santana
Everton Santana@Evertontag·
@PythonPr Python students seeing .copy() for the first time be like: ‘Wait… why didn’t it change too?’
English
0
0
3
634
Biel Men
Biel Men@BielMenHaha·
@PythonPr Will be [1,2,3] After the copy, a new list is allocated in another memory address with the same items, but original remains at the same address.
English
0
0
2
514
Nick Shilov
Nick Shilov@gaussdevhere·
@PythonPr A. Since y is an independent copy of subsistent changes on x so it’s [1,2,3]
English
0
0
1
473
Bassem
Bassem@Bassem_Oman·
@PythonPr A. ✅ print(x) [1, 2, 3, 4]
English
0
0
1
226
Perturabowl 🦉🪶🥚
Perturabowl 🦉🪶🥚@lonelyowl113·
@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
1
0
1
66
AA
AA@ikd_ii·
@PythonPr A) because the y is new list with x values if y=x then y is reference to x
English
0
0
1
81
Paylaş