
In Python, both is
and ==
are comparison operators, but they serve different purposes.
๐น Main Difference
Operator | Purpose | Compares |
---|---|---|
== | Value Equality | Checks if two objects have the same value/content |
is | Identity Equality | Checks if two variables refer to the same object (same memory location) |
๐น Example
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True โ values are equal
print(a is b) # False โ different objects in memory
print(a is c) # True โ same object in memory
๐น Output
True
False
True
๐น Rule of Thumb
- Use
==
โ When you want to check if two objects have the same value. - Use
is
โ When you want to check if two variables refer to the same object in memory.
โ Summary:
==
โ Compares values.is
โ Compares identity (memory location).