0

Why are variables entangled?

a=[1,2,3] b=a b.append(4) Print(a) Why does a now = b? This confuses me, if i had an apple and cloned it. Took a bite of the clone the original wouldnt have a bite in it. Can someone please explain?

25th Jul 2019, 7:03 PM
James Wray
1 ответ
+ 2
The names for variables in Python are like name stickers. a = [1, 2, 3] This means: 'Please give me this list, and let me call it a.' b = a This means: 'Please give me a and let me call it b.' It is the same list, you just have added a second sticker to it. If you want to clone the list, you need to create a *new* list that looks like the old one. For example like this: b = list(a) This means: 'Give me a list with the values from a and let me call it b.'
25th Jul 2019, 7:18 PM
HonFu
HonFu - avatar