01 / 21

Equals vs. Is
in Python

Learn to distinguish == (equality) from is (identity): two operators that look similar but test fundamentally different things.

Python

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.

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

Python
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

== (Equality) a [1, 2, 3] id: 0x1A b [1, 2, 3] id: 0x2B a == b → True a is b → False is (Identity) c [1, 2, 3] id: 0x1A d c == d → True c is d → True Two separate objects, same value One shared object, two names

The Core Rule

== checks value equality
Calls the object's __eq__ method. Two different objects can be equal if their contents match.
is checks object identity
Compares memory addresses via id(). 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.

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

Python
# 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

Use == for
Comparing values: numbers, strings, lists, dicts, custom objects. This is what you want 99% of the time.
Use is for
Checking against singletons: None, True, False. The idiomatic Python pattern is if x is None.

How to Decide Which Operator

1
Ask: "Am I comparing to None?" If yes, use is None or is not None.
2
Ask: "Do I care about the value?" If you want to know whether two things hold the same data, use ==.
3
Ask: "Do I need to verify same object?" Rare cases like caching, sentinel objects, or debugging. Use is.
4
When in doubt, use ==. It is almost always the right choice for comparing data.

Example 1: Lists

Python
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

Python
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

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

Immutable
int, str, tuple, float, bool, frozenset. Python may cache and reuse these objects.
Mutable
list, dict, set, custom objects. Python always creates a new object. is is False unless explicitly aliased.

Common Mistakes

✕ Using is to compare values
Writing if x is 10 instead of if x == 10. This may work for small integers due to interning, but breaks for larger values.
✕ Using == for None
Writing if x == None instead of if x is None. A custom __eq__ could make this return unexpected results.
✕ Assuming is is faster
While is 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?

Question 2 of 5

Given a = [1, 2] and b = [1, 2], what do a == b and a is b return?

Question 3 of 5

What is the Pythonic way to check if a variable is None?

Question 4 of 5

Why might a = 100; b = 100; a is b return True, even though a and b are assigned separately?

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)