0

if text1 = text2 is True then why textlist1 != textlist 2 ?

text1 = "korea" text2 = "korea" textlist1 =[1, 2, 3] textlist2 =[1, 2, 3] print(text1 is text2) print(textlist1 is textlist2)

6th Aug 2020, 11:25 AM
Yun Jo Subba
Yun Jo Subba - avatar
4 Respostas
+ 5
Attach the following code to your code to get some understanding how python stores variables. print(id(text1)) print(id(text2)) print(id(textlist1)) print(id(textlist2)) Also, check HonFu codes because he has a very good explanation amongst them
6th Aug 2020, 11:31 AM
Rik Wittkopp
Rik Wittkopp - avatar
6th Aug 2020, 11:40 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 3
An array is an object, and when comparing two objects it checks if they are the same object, it doesn't check the values inside the array. Try running this code: arr1 = [1, 2, 5] arr2 = arr1 arr2[0] = 7 print(arr1) You should see that when you assigned arr1 to arr2 and changed arr2, the values also changed in arr1, because they refer to the same object.
6th Aug 2020, 11:31 AM
coddy
coddy - avatar
6th Aug 2020, 11:42 AM
Rik Wittkopp
Rik Wittkopp - avatar