+ 1

Need help with the 'is' operator

Can someone please tell me why: "p" is "p" -- > True but. [] is [] --> Flase

12th Jul 2019, 8:31 PM
Adarsh Namdev
Adarsh Namdev - avatar
2 odpowiedzi
+ 1
The “is” operator checks whether or not the two operrands are the same object, where as the “==“ operator checks if the two operands are of the same type. “[] is []” is the same as saying list1 is list2, which is false because they are not the same objects, unless you were to explicitly declare list2 = list1, making them the same object. If you were to type “[] == []” that would return true because they are the same type, which is list.
12th Jul 2019, 9:16 PM
Jake
Jake - avatar
+ 1
Strings are an immutable type. For these, Python is allowed to store just one value 'p' for the whole program, because nothing bad can happen. For lists, which are mutable objects, this must not happen - if you create two lists independently of another, they need to be two separate lists. Because you could change one of them later, and if it was one list, the 'other' would change too without you even knowing.
12th Jul 2019, 10:22 PM
HonFu
HonFu - avatar