Comparison Operators in Python
Comparison operators compare two values and always produce a boolean result. Python has six: == (equal), != (not equal), < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal). They work on numbers, and also on strings, which are compared alphabetically.
== checks equality
Different from = (assignment) — one of the most common beginner mix-ups.
!= checks inequality
True whenever the two values differ.
<, >, <=, >= compare order
Numerically for numbers, alphabetically (character by character) for strings.
Comparisons can be chained
1 < x < 10 checks both conditions in a single, natural-reading expression.
Comparing unrelated types
== simply returns False rather than erroring; ordering comparisons like < raise a TypeError instead of guessing.
print(5 == 5)print(5 != 3)print("apple" < "banana")print(1 < 5 < 10)Equality (==) vs Assignment (=)
One of the most common beginner mix-ups — = assigns a value, == checks whether two values are equal.
x = 5print(x == 5)Chained Comparisons
Python lets you chain comparisons naturally, unlike many other languages.
age = 25print(18 <= age <= 65)Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.