Questions about massive | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Questions about massive

Hello guys, sorry for my English and stupidity :) here is the question: num1=[1,2,3,4] num2=num1 num2[0]=9 print ("num1 is: ", num1, "\nnum2 is: ", num2) why if I change one of them (num1 or num2) they both change? num1[0]=0 and num2[0]=0 too. Why does it happens? I've changed only num2, not num1 Thank you in advance

1st Sep 2017, 5:49 PM
Barmalew
4 Answers
+ 1
python works with a method called object sharing. this means that when you assign a variable to be another one, it will exactly be the same object. you need to make a copy in order to do what you want, i dont recall the functions name, but it should not be very difficult to search for it. edit: here you go, https://docs.python.org/2/library/copy.html
1st Sep 2017, 5:54 PM
Testing003
+ 1
a number is not an object. a list or set is though. you should read the python docs. it is all well explained
1st Sep 2017, 7:15 PM
Testing003
0
To Testing003 Ok then. But if I follow the same logic for this code a=2 b=a a=7 print(b) I will get result b=7, but it is false. True is that b = 2 (((
1st Sep 2017, 7:13 PM
Barmalew
0
To Testing003. Ok. Thank you very much! Sorry about this stupid question please :)
1st Sep 2017, 7:16 PM
Barmalew