Comparison Operators
\(\text{&}\) Chaining
Master Python's comparison operators and learn to write elegant chained comparisons like \( 0 < x \leq 10 \).
What Are Comparison Operators?
Comparison operators let you compare two values and get back a Boolean: either True or False. They are the foundation of every if-statement, while-loop, and filter you will ever write in Python.
The Six Comparison Operators
==!=<><=>=Visualizing Comparisons on a Number Line
A value on the left of the number line is less than a value on the right. Python simply checks their positions.
= vs ==: A Critical Distinction
| = (Assignment) | == (Comparison) | |
|---|---|---|
| Purpose | Stores a value | Checks equality |
| Returns | Nothing (statement) | True or False |
| Example | x = 5 |
x == 5 |
| Read as | "Set x to 5" | "Is x equal to 5?" |
How Python Evaluates Comparisons
Introducing Chained Comparisons
In many languages, to check if \( x \) is between 0 and 10 you must write two separate comparisons joined by and. Python lets you chain them naturally:
How Chaining Works Under the Hood
Comparing Different Types
int, float mix freely: 3 == 3.0 is True"apple" < "banana" is True"5" == 5 is False. Python does not auto-convert strings to numbers.How to Apply Comparisons
== for equality, </> for strict order, <=/>= if the boundary is included.and.x < 10 or x <= 10?Example 1: Simple Comparison
Example 2: Chained Range Check
Example 3: Multi-Chain Validation
Expansion: (65 <= 72 and 72 <= 80) → True and (30 <= 45 and 45 <= 60) → True. Final: True.
Common Mistakes
if x = 5: causes a SyntaxError. Always use == for equality checks."10" > 5 raises TypeError. Convert first: int("10") > 5.x < 10 excludes 10. If 10 should be valid, use x <= 10. Off-by-one errors are sneaky.Summary Reference Table
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal | 5 != 3 |
True |
< |
Less than | 3 < 5 |
True |
> |
Greater than | 3 > 5 |
False |
<= |
Less or equal | 5 <= 5 |
True |
>= |
Greater or equal | 4 >= 5 |
False |
| \( a < x < b \) | Chained range | 0 < 5 < 10 |
True |
Question 1 of 5
What does the expression 7 <= 7 evaluate to in Python?
<= operator checks "less than or equal to." Since 7 equals 7, the condition is satisfied and the result is True.Question 2 of 5
Which of the following is the correct Pythonic way to check if \(x\) is between 1 and 100 (inclusive)?
1 <= x <= 100 is a chained comparison that checks \( 1 \leq x \) and \( x \leq 100 \). Option A excludes the endpoints. Option B excludes both 1 and 100. Option D uses or, which is always True for any number.Question 3 of 5
What is the output of the following code?
x = 5
print(1 < x < 3)
1 < 5 < 3 to 1 < 5 and 5 < 3. The first part is True, but 5 < 3 is False. True and False = False.Question 4 of 5
What does "apple" < "banana" evaluate to?
"a" comes before "b", "apple" < "banana" is True.Question 5 of 5
How many times is the variable \(x\) evaluated in the chained comparison \(a < x < b\)?
x appears in both a < x and x < b, Python evaluates it a single time. This matters when x is a function call with side effects.