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).
