Identity operator compares the memory locations of two objects.
Python supports the following Identity Operators:
- IS Operator
- IS NOT Operator
IS Operator
This operator returns True if operands or value on both sides of the operator point to the same object and False otherwise.
For example, if a is b returns 1, if id(a) is the same as id(b)
Example 1: IS Operator:
a=60 b=90 if(a is b): print("a and b have same identity") else: print("a and b do not have same identity")
Output:
a and b do not have the same identity
Example 2: IS Operator:
a=60 b=60 if(a is b): print("a and b have same identity") else: print("a and b do not have same identity")
Output:
a and b have the same identity
Example 3: IS Operator:
a=60 b=60 if(id(a)==id(b)): print("a and b have same identity") else: print("a and b do not have same identity")
Output:
a and b have the same identity
IS NOT Operator
This operator returns True if the operand or value on both sides of the operator does not point to the same object and False otherwise.
For example,
If a is not b returns 1 if id(a) is not the same as id(b).
Example 1: IS NOT Operator:
a=60 b=80 if(a is not b): print("a and b do not have same identity") else: print("a and b have same identity")
Output:
a and b do not have the same identity
Leave a comment