Category: python
Identity vs Equality
Published on 27 Oct 2026
Explanation
== compares values; is checks whether two references identify the same object.
Code:
a = [1, 2] b = [1, 2] print(a == b, a is b)
Explanation
Lists with equal contents may be separate objects. Mutating one does not change the other unless they share a reference.
Code:
first = [10, 20] second = [10, 20] second.append(30) print(first == second)
Explanation
Use is None or is not None to check for the special None object.
Code:
result = None
if result is None:
print('No result')
Explanation
Do not use is to compare ordinary numbers or strings; implementation optimizations can make identity behavior unpredictable.
Code:
a = 1000 b = 1000 print(a == b) # Compare values with ==
Explanation
Separate instances are not identical. Unless equality is customized, separate instances are also unequal with ==.
Code:
class Student:
def __init__(self, name): self.name = name
a = Student('Arun')
b = Student('Arun')
print(a is b, a == b)