+ 4

Why it's True False False?

Plaese take a look at my Python code. Can you explain me the 'is' operator in this case? https://code.sololearn.com/c88nbF0EdLW2/?ref=app

2nd Jun 2020, 7:00 AM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar
8 Respuestas
+ 7
list1 = [1,2] >create a list and store it in list1 list2 = list1 >assign list1 to list2..meaning list2 is also pointing to same object(i think python call value as object)..so there are both pointing to same thing list3 = list2[:] >that[:] in list will return a new list..it will have same value as list2 but it is a new one.. "is" keyword will only return true if u compare two variable that point to same object
2nd Jun 2020, 7:08 AM
durian
durian - avatar
+ 6
The id is different, it is another object, try this adding to your code. print (id(list1)) print (id(list2)) print (id(list3))
2nd Jun 2020, 7:04 AM
Paul
Paul - avatar
+ 4
Okay Lily Mea final test: L1 = [[1,1],[1,2]] L2 =L1[:] L1[0][0]=3 What is L2 now and why?
2nd Jun 2020, 7:31 AM
Oma Falk
Oma Falk - avatar
+ 3
Additionally to HonFu s great tutorial I would propose that you read all functions for a list object (Sort,reverse,....) and find out if the result creates a new reference or works on list itself(inplace).
2nd Jun 2020, 7:21 AM
Oma Falk
Oma Falk - avatar
+ 2
After reading this, all of that sort of questions should become clear... I hope. https://code.sololearn.com/c89ejW97QsTN/?ref=app
2nd Jun 2020, 7:10 AM
HonFu
HonFu - avatar
+ 1
Lily Mea Now I understand the 'is' operator. It compares the ID of the variable and not its value. Thanks for explanation to all answers
2nd Jun 2020, 7:22 AM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar