Equals vs. Is
in Python
Learn to distinguish == (equality) from is (identity): two operators that look similar but test fundamentally different things.
What Does "Equal" Really Mean?
In everyday life, saying two things are "equal" usually means they have the same value. Two $5 bills are equal in value, even though they are physically different pieces of paper.
In Python, equality and identity are two separate concepts. Understanding the difference is critical to writing correct, bug-free code.
The == Operator: Equality
The == operator compares the values of two objects. If the values are the same, it returns True, regardless of whether they are stored at the same memory location.
a = [1, 2, 3] b = [1, 2, 3] print(a == b) # True — same values print(a is b) # False — different objects
The is Operator: Identity
The is operator checks whether two variables point to the exact same object in memory. It does not care about values: it checks the object's identity (its id()).
a = [1, 2, 3] b = a # b now points to the SAME object print(a == b) # True — same values print(a is b) # True — same object! print(id(a) == id(b)) # True
Visualizing Memory
The Core Rule
== checks value equality__eq__ method. Two different objects can be equal if their contents match.is checks object identityid(). Only True when both names reference the exact same object.Under the Hood: id()
Every Python object has a unique integer identifier, returned by id(). The is operator is equivalent to comparing these IDs.
x = "hello" y = "hello" print(id(x)) # e.g. 140234866718384 print(id(y)) # e.g. 140234866718384 (same!) # The 'is' operator does this internally: print(id(x) == id(y)) # True print(x is y) # True
The Interning Trap
CPython caches small integers (-5 to 256) and certain strings. This means is can return True for these values, tricking you into thinking is and == are the same.
# Small integers: interned (cached) a = 100 b = 100 print(a is b) # True (cached!) # Large integers: NOT interned a = 1000 b = 1000 print(a is b) # False (different objects) print(a == b) # True (same value)
When to Use == vs. is
== foris forNone, True, False. The idiomatic Python pattern is if x is None.How to Decide Which Operator
is None or is not None.==.is.==. It is almost always the right choice for comparing data.Example 1: Lists
fruits = ["apple", "banana"] basket = ["apple", "banana"] alias = fruits # Value comparison print(fruits == basket) # True (same contents) print(fruits == alias) # True (same contents) # Identity comparison print(fruits is basket) # False (different objects) print(fruits is alias) # True (same object!)
Example 2: Checking for None
def find_user(username): # Returns user dict or None if not found if username == "admin": return {"name": "Admin", "role": "superuser"} return None result = find_user("guest") # ✓ Correct: use 'is' for None if result is None: print("User not found") # ✕ Avoid: use '==' for None if result == None: # Works, but not Pythonic print("User not found")
Example 3: Custom Objects
class Point: def __init__(self, x, y): self.x, self.y = x, y def __eq__(self, other): return self.x == other.x and self.y == other.y p1 = Point(3, 4) p2 = Point(3, 4) print(p1 == p2) # True (values match via __eq__) print(p1 is p2) # False (two separate instances)
Mutable vs. Immutable Types
Whether Python reuses objects depends on whether the type is mutable or immutable. This affects when is returns True unexpectedly.
int, str, tuple, float, bool, frozenset. Python may cache and reuse these objects.list, dict, set, custom objects. Python always creates a new object. is is False unless explicitly aliased.Common Mistakes
is to compare valuesif x is 10 instead of if x == 10. This may work for small integers due to interning, but breaks for larger values.== for Noneif x == None instead of if x is None. A custom __eq__ could make this return unexpected results.is is fasteris is technically faster, using it for value comparison introduces subtle bugs. Correctness beats micro-optimization.Summary Comparison
| == (Equality) | is (Identity) | |
|---|---|---|
| Tests | Same value | Same object |
| Mechanism | Calls __eq__ |
Compares id() |
| Can be overridden? | Yes | No |
| Use case | Value comparison (default) | None / singleton checks |
[1,2] ? [1,2] |
True | False |
| Affected by interning? | No | Yes |
Question 1 of 5
What does the == operator check in Python?
== invokes the __eq__ method to compare the values of two objects, not their memory locations or types.Question 2 of 5
Given a = [1, 2] and b = [1, 2], what do a == b and a is b return?
== returns True. But they are separate objects in memory, so is returns False.Question 3 of 5
What is the Pythonic way to check if a variable is None?
None is a singleton, so you should use is to check identity. Using == could give wrong results if an object overrides __eq__. Option C is a syntax error (assignment, not comparison). Option D would also match 0, "", and [].Question 4 of 5
Why might a = 100; b = 100; a is b return True, even though a and b are assigned separately?
a = 100, Python reuses the cached object, so a and b end up pointing to the same object. This does not work for larger integers like 1000.Question 5 of 5
Given this class, what will the final print output?
class Box: def __eq__(self, other): return True b1 = Box() b2 = Box() print(b1 == b2, b1 is b2)
__eq__ always returns True, so b1 == b2 is True. But b1 and b2 are different instances, so b1 is b2 is False. You cannot override is.