Every conditional in your code — every if, every loop that runs "while" something is true — depends on python comparison operators. They're deceptively simple on the surface, but a couple of them (particularly == versus is) trip up even developers who've been writing Python for a while. This article covers all six comparison operators, how they behave across different data types, and the identity-vs-equality distinction that causes so much confusion.
What Are Comparison Operators?
A comparison operator takes two values and always returns a Boolean: True or False. They're also called relational operators, since they describe the relationship between two values — equal, greater, less, and so on.
print(5 > 3) # TrueThese operators are the foundation of conditional logic throughout Python — every if statement, every while loop condition, ultimately comes down to a comparison like this one.
Python has six comparison operators:
== # equal to
!= # not equal to
> # greater than
< # less than
>= # greater than or equal to
<= # less than or equal toThe Six Comparison Operators, One by One
== (equal to)
Checks whether two values are equal:
print(5 == 5) # True
print(5 == 3) # FalseThe classic beginner mistake: confusing == (comparison) with = (assignment). This is one of the most common early errors:
x = 5
if x = 5: # SyntaxError — this should be ==
print("x is 5")Python catches this particular mistake with a syntax error rather than silently doing the wrong thing, which is a small mercy — but it's worth burning the distinction into memory early: single = assigns, double == compares.
!= (not equal to)
print(5 != 3) # True
print(5 != 5) # False>, <, >=, <=
print(5 > 3) # True — greater than
print(5 < 3) # False — less than
print(5 >= 5) # True — greater than or equal to
print(5 <= 4) # False — less than or equal toBonus: python string comparison
Strings can be compared with these same operators, evaluated lexicographically — essentially, alphabetical order, based on the underlying character codes:
print("apple" < "banana") # True — 'a' comes before 'b'
print("apple" == "apple") # True
print("Apple" == "apple") # False — comparison is case-sensitiveThat last example is worth remembering: string comparison is case-sensitive by default, since uppercase and lowercase letters have different underlying character codes. If you need a case-insensitive comparison, convert both sides with .lower() or .upper() first.
Comparing Different Data Types
Compatible numeric types
Comparisons work seamlessly across compatible numeric types, the same way arithmetic does:
print(5 == 5.0) # True — int and float compare by value
print(5 < 5.5) # TrueIncompatible types raise errors
Equality checks (==, !=) are lenient — they'll compare almost anything and simply return False if the types don't match in any meaningful way, rather than raising an error:
print(5 == "5") # False — no error, just not equal
print([1, 2] == "hello") # FalseBut ordering operators (<, >, <=, >=) are stricter. Trying to determine whether one incompatible type is "greater than" another often raises a TypeError, since there's no sensible way to order them:
print(5 < "5")
# TypeError: '<' not supported between instances of 'int' and 'str'The general rule: == and != work broadly across almost any two values, while <, >, <=, and >= require the values to be meaningfully orderable — usually meaning they need to be the same type, or at least compatible numeric types.
Chaining Comparisons
Python allows a genuinely elegant shorthand that a lot of other languages don't support: chaining multiple comparisons in a single, readable expression.
number = 15
# Chained comparison — reads naturally
if 10 <= number <= 20:
print("Number is in range")This is equivalent to writing it out with and, but noticeably cleaner:
# The long-hand equivalent
if 10 <= number and number <= 20:
print("Number is in range")Both produce identical behavior, but the chained version reads almost exactly like the mathematical notation you'd write by hand, and it's the idiomatic way to express a range check in Python.
Practical use cases
Chained comparisons show up constantly in validation logic:
age = 25
if 18 <= age <= 65:
print("Eligible")
score = 87
if 90 <= score <= 100:
grade = "A"
elif 80 <= score <= 89:
grade = "B"
else:
grade = "C or below"== vs. is: A Common Point of Confusion
This is the distinction that trips up more people than any other comparison-related topic in Python, and it's worth slowing down for.
Value equality vs. object identity
== checks whether two values are equal. is checks whether two variables refer to the exact same object in memory — identity, not equivalence.
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True — same values
print(a is b) # False — two separate list objects, even though they look identicala and b hold lists that look the same, but they're two distinct objects sitting at different locations in memory. == correctly reports they're equal in value; is correctly reports they're not the same object.
The integer caching gotcha
Here's a detail that genuinely surprises people, including intermediate developers: for performance reasons, CPython (the standard Python implementation) caches and reuses small integers, roughly in the range of -5 to 256. This means small integers can appear to pass an is check even without you intending an identity comparison:
a = 256
b = 256
print(a is b) # True — small integers are cached and reused
x = 257
y = 257
print(x is y) # False — outside the cached range, these are separate objectsThis isn't something you should ever rely on intentionally — it's an internal implementation detail of CPython, not a guaranteed language feature, and it can behave differently in other Python implementations or even different circumstances within CPython itself. It's worth knowing about mainly so that if you ever see is unexpectedly return True for two integers, you understand why, rather than assuming your code is doing something it isn't.
Best practice
The rule to actually follow: use == for comparing values, which is the overwhelming majority of comparisons you'll ever write. Reserve is specifically for singleton checks — most notably, checking against None:
if x is None: # correct — None is a singleton
print("x has no value")
if x == 5: # correct — comparing an actual value
print("x is five")Using is for general value comparisons (especially with numbers, strings, or lists) is a common source of subtle bugs precisely because of quirks like the integer caching behavior above — the code might happen to work today, on this particular Python implementation, and then silently break somewhere else.