Identity Operators
Use the Identity operator to check whether the value of two variables is the same or not. This operator is known as a reference-quality operator because the identity operator compares the values in the memory addresses of two variables. There are two identity operators.
is: Theisoperator returns boolean values eitherTrueorFalse. It returnsTrueif the memory address's first value is equal to the second value. Otherwise, it returnsFalse.is not: Theis notoperator returns boolean values eitherTrueorFalse. It returnsTrueif the first value is not equal to the second value. Otherwise, it returnsFalse.
| Operator | Description | Example | Result | Condition |
|---|---|---|---|---|
is |
Is | a is b |
True / False |
True if both point to same object in memory |
is not |
Is not | a is not b |
True / False |
True if they are different objects in memory |
Example:
x = 10
y = 11
z = 10
print(x is y) # it compare memory address of x and y
print(x is z) # it compare memory address of x and z
print(x is not y) # it compare memory address of x and y
print(x is not z) # it compare memory address of x and z
Output:
False
True
True
False