+ 1
Need help with the 'is' operator
Can someone please tell me why: "p" is "p" -- > True but. [] is [] --> Flase
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.
+ 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.