Comparison operators compare two values and return a booleanTrue if the comparison holds, False if it doesn’t.

The six operators

a: int = 5
b: int = 10

print(a == b)    # False  equal
print(a != b)    # True   not equal
print(a < b)     # True   less than
print(a > b)     # False  greater than
print(a <= b)    # True   less than or equal
print(a >= b)    # False  greater than or equal

Most are obvious. Two things to watch:

  • == (two equals signs) means “is equal to”. A single = means assignment, not comparison.
  • != means “not equal”.

Comparing different types

You can compare numbers across types — int and float compare cleanly:

print(5 == 5.0)    # True
print(3 < 3.5)     # True

Strings compare alphabetically (more precisely, by character codes):

print("apple" < "banana")   # True
print("Apple" < "apple")    # True — uppercase letters come first in ASCII
print("apple" == "Apple")   # False — case matters

But comparing across unrelated types (e.g. a string and a number) raises an error:

"5" < 5   # TypeError: '<' not supported between str and int

That’s a good thing. Older languages would silently produce a result that often surprises you. Python prefers to stop and tell you.

Chained comparisons

Python lets you chain comparisons in a way most languages don’t:

age: int = 25

if 18 <= age < 65:
    print("Adult")

18 <= age < 65 reads exactly how it sounds: “age is between 18 (inclusive) and 65 (exclusive)”. This is a Python-specific feature, and it’s much clearer than age >= 18 and age < 65.

Comparing floats — the gotcha

We met this in Section 2. It bears repeating because it’s the single most common bug for newcomers:

print(0.1 + 0.2 == 0.3)   # False!

Floats can’t represent all decimals exactly. Use a tolerance instead:

print(abs((0.1 + 0.2) - 0.3) < 1e-9)   # True

For comparing tensor values in ML libraries, you’ll use NumPy’s np.allclose() or PyTorch’s torch.allclose() — same idea, built in.

== vs is

There are two ways to compare in Python, and they mean different things:

  • == asks “do these two values look the same?”
  • is asks “are these two variables pointing to the exact same object?”
a: list[int] = [1, 2, 3]
b: list[int] = [1, 2, 3]
c: list[int] = a

print(a == b)   # True  — they have the same contents
print(a is b)   # False — they're separate objects in memory
print(a is c)   # True  — c and a are the same object

Most of the time you want ==. The one place to always use is:

if x is None:
    ...

None is a single special value. There’s only ever one of it. So is None is the idiomatic Python check.

Comparison and assignment look similar — careful!

A single = does assignment, not comparison:

x = 5     # assigns 5 to x
x == 5    # checks if x equals 5 (returns True or False)

Mixing them up is a classic typo:

# bug
if x = 5:   # SyntaxError: invalid syntax
    ...

# correct
if x == 5:
    ...

Python catches this for you (some languages don’t), but it’s still worth being deliberate.

What’s next

You can ask “is this value bigger than that one?”. Next, logical operators — combining comparisons with and, or, and not.

Toggle theme (T)