Mastering Comparison Operators in Python
Understanding comparison operators is fundamental to writing logical conditions in Python. Whether you're checking if values are equal, comparing numbers, or combining multiple comparisons into a single expression, these tools help control the flow of your program with precision.
In this blog, we’ll cover:
- 
Basic comparison operators (
==,!=,>,<,>=,<=) - 
Chained comparison operators
 - 
Logical operators:
and,or - 
Real Python examples and breakdowns
 
Basic Comparison Operators
These operators allow you to compare two values and return a Boolean result — True or False. If you’ve done basic math before, you’ll feel right at home.
Let’s assume:
a = 3
b = 4
| Operator | Description | Example | Output | 
|---|---|---|---|
== | 
Equal to | a == b | 
False | 
!= | 
Not equal to | a != b | 
True | 
> | 
Greater than | a > b | 
False | 
< | 
Less than | a < b | 
True | 
>= | 
Greater than or equal to | a >= b | 
False | 
<= | 
Less than or equal to | a <= b | 
True | 
Examples of Basic Comparison Operators
Equal (==)
2 == 2  # True
1 == 0  # False
==compares values. Do not confuse it with=which assigns a value.
Not Equal (!=)
2 != 1  # True
2 != 2  # False
Greater Than (>)
2 > 1  # True
2 > 4  # False
Less Than (<)
2 < 4  # True
2 < 1  # False
Greater Than or Equal To (>=)
2 >= 2  # True
2 >= 1  # True
Less Than or Equal To (<=)
2 <= 2  # True
2 <= 4  # True
Chained Comparison Operators in Python
Python allows chained comparisons, which make expressions cleaner and more readable than using multiple and statements.
Example 1: Simple Chain
1 < 2 < 3  # True
This checks if 1 < 2 and 2 < 3. You could write:
1 < 2 and 2 < 3
Example 2: More Complex Chain
1 > 6 < 9 > 3  # False
Which is the same as:
1 > 6 and 6 < 9 and 9 > 3
# False     True      True → Overall: False
Example 3: Chain with Mixed Operators
1 < 3 > 2  # True
Which evaluates as:
1 < 3 and 3 > 2
Using and & or in Comparisons
Python also supports combining logical conditions using the and and or keywords.
and Operator
Both conditions must be True for the entire expression to be True.
1 < 2 and 2 < 3  # True
1 > 5 and 2 < 3  # False
or Operator
Only one of the conditions needs to be True.
1 == 2 or 2 < 3  # True
1 == 1 or 100 == 1  # True
Practice and Breakdown
Let’s analyze a few expressions:
Example 1:
1 < 6 and 6 < 9 or 9 < 3
# → True and True or False
# → True or False
# → True
Example 2:
1 < 6 or 6 < 9 and 9 < 3
# → True or True and False
# → True or False
# → True
Example 3:
1 > 6 and 6 < 9 or 9 < 3
# → False and True or False
# → False or False
# → False
Final Thoughts
You’ve now learned:
- 
How to use comparison operators in Python
 - 
How chaining comparisons makes code cleaner
 - 
The importance of
andandorin logical expressions 
Take time to practice these concepts, as they’re foundational to conditions, loops, and control flow in Python.