0
Why this is different?
for example a=12 b=a a=7 then when i call b, the output will be 12 but if something like this a=[1,2,3] b=a a[1]=7 then if i call b [1,7,3] is it because the second codes just change a part of the list unlike the first one which change whole value of var a
3 Answers
+ 3
For the first example, the variables are *passed by value*. Explanation:
a = 12 #a = 12
b = a #a = 12, b = 12
a = 7 #a = 7, b = 12
so b is 12. Simple? Alright, on to the second one.
Lists (or arrays as I am used to calling them) are *passed by reference*. Explanation:
a = [1, 2, 3] #a = [1, 2, 3]
b = a #a = [1, 2, 3], b = [1, 2, 3]
a[1] = 7 #a = [1, 7, 3], b = [1, 7, 3]
As a is a list and *b is assigned to a*, both are effectively the same list. So if you change a you change b. If you understand pointers in C++ and other languages, this is because a and b are pointing to the same memory location.
Hope this helped. š
+ 1
Effectively, yes.
0
mm so that's how it was..
thank you very much all..
i guess i understand now