- == (Equality)
- Take an example of identical twins. Look wise they are exactly same (equal).
- Expression evaluates to True if two objects (referred by variables) have the same contents (or are equal).
- is (Identity)
- Take an example of identical twins. Both are different human beings.
- Expression evaluates to True if two variables point to the same (identical) object.
>>> a = [1,2,3]
>>> id(a)
4557217944
>>> b = a
>>> id(b)
4557217944
>>> a == b
True
>>> a is b
True
Note: Here a is a list, which has a particular identity (4557217944), and we are assigning a to b, so b has the same identity as a (4557217944).
>>> a = [1,2,3]
>>> id(a)
4557293400
>>> b = [1,2,3]
>>> id(b)
4557140408
>>> a == b
True
>>> a is b
False
Note: Here a is a list, which has a particular identity (4557293400), and we are creating a new list b, so b has a different identity (4557140408) than a's identity (4557293400).
No comments:
Post a Comment