Hello, can anyone explain this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hello, can anyone explain this?

a = [1,2,3] b = a a.append(4) print(b) #[1,2,3,4] print(a) #[1,2,3,4] Till now they share the same memory location but when I write: a = a + [5] print(b) #[1,2,3,4] print(a) #[1,2,3,4,5] Now, they no more share the same memory location!! Why does this happen?

2nd Aug 2019, 12:55 PM
Stravo1
Stravo1 - avatar
7 Answers
+ 3
Stravo1 okay earlier i thought that both the codes are different but they are not as you told me here pay attention to the line a=a+[4] and a.append(4) let's see the difference when you do a.append(4) what Python does is that i add 4 to the same list which is pointed by a and b both names therefore in append case when you print a or b it shows the same output but in a=a+[4] what Python does is that as this line a=a+[4] is executed by the Python interpret the memory pointer of memory location 'a' creates a new memory location for the variable a in which the old list data and the newly added data is copied and the variable b is still pointing the old memory location but now variable a is pointing a new memory location see the difference here you did not commanded the Python to append the element in the same list but you commanded Python to add a element to list a so what Python does is that it creates a new list with old data and add new data in that new list
2nd Aug 2019, 5:58 PM
Khan Ayaan
Khan Ayaan - avatar
+ 2
To Understand This You Must Know How The Assignment Operator (=) Works in The Memory Let me explain you About the assignment operator. when you declare a variable in memory it must have a name such as in your case a list type data structures have name 'a' and this name 'a' points the data structures list location in memory but when you does b=a Python does not create another data structures of list type with name 'b' But what Python does is that it gives name 'b' to that old data structure 'a' so basically 'a' and 'b' both are pointing the same Memory location it is actually same as calling a single persona with two names. in context with Python it can be said that a single Memory location is pointed by two names.
2nd Aug 2019, 5:27 PM
Khan Ayaan
Khan Ayaan - avatar
0
Khan Ayaan ...okay, I got your point but how does this explain the last part of my question? There too I have two names pointing to one location, isn't it??
2nd Aug 2019, 5:30 PM
Stravo1
Stravo1 - avatar
0
no no there is not that case in the last part you have added one Element in a by a=a+[5] but you did not created a relationship between a and b as you had done that in the upper case by writing b=a and in the last case variable a and b both are pointing the different memory locations in the memory and the changes made in b will not affect a and same goes for a that is changes made in a will not affect b unless you create a relationship between then by assigning a to b by b=a or by assigning b to a by a=b.
2nd Aug 2019, 5:35 PM
Khan Ayaan
Khan Ayaan - avatar
0
Khan Ayaan Okay...(actually the 2nd part of the code is a continuation of the 1st part where I've mentioned b=a) but still if I say: a=[1,2,3] b=a a=a + [4] print(a) #[1,2,3,4] print (b) #[1,2,3] How do you explain this? Here too 'b' is linked to 'a', and just like in the previous code [a.append(4)] I'm adding 4 to 'a' [instead of append() I'm using +], but unlike the previous code any change in 'a' this time won't affect 'b'? Why?
2nd Aug 2019, 5:42 PM
Stravo1
Stravo1 - avatar
0
Khan Ayaan Okay, perfect answer... Thnx Happy coding 😄😄
2nd Aug 2019, 6:02 PM
Stravo1
Stravo1 - avatar
0
Stravo1 Welcome Enjoy Coding.
2nd Aug 2019, 6:04 PM
Khan Ayaan
Khan Ayaan - avatar