01 / 20
\(\le\) \(\ge\)

Comparison Operators
\(\text{&}\) Chaining

Master Python's comparison operators and learn to write elegant chained comparisons like \( 0 < x \leq 10 \).

Python

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.

>>> 5 > 3 True >>> 2 == 7 False

The Six Comparison Operators

==
Equal to
!=
Not equal to
<
Less than
>
Greater than
<=
Less than or equal
>=
Greater than or equal

Visualizing Comparisons on a Number Line

0 3 5 7 10 \(a = 3\) \(b = 5\) \(a < b\) \(3 < 5 \rightarrow \text{True}\)

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

\(x \ge 10\) Look up value of x Compare: is value \(\ge 10\)? True False

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:

# The long way if x > 0 and x <= 10: print("In range") # The Pythonic way (chained) if 0 < x <= 10: print("In range")

How Chaining Works Under the Hood

\(1 < x \le 5 < y\) Python internally expands to: \(1 < x\) and \(x \le 5\) and \(5 < y\) Each sub-expression is evaluated left to right. Short-circuit: stops at the first False.

Comparing Different Types

Numbers
int, float mix freely: 3 == 3.0 is True
Strings
Compared lexicographically (dictionary order): "apple" < "banana" is True
Mixed Types
"5" == 5 is False. Python does not auto-convert strings to numbers.

How to Apply Comparisons

1
Identify the question. What relationship are you testing? Equality, ordering, or range?
2
Pick the right operator. Use == for equality, </> for strict order, <=/>= if the boundary is included.
3
Chain when possible. If testing a range like \( a < x < b \), write it as one chained expression instead of two with and.
4
Test edge cases. What happens at the boundary? Does \( x = 10 \) satisfy x < 10 or x <= 10?

Example 1: Simple Comparison

age = 17 if age >= 18: print("You can vote!") else: print("Not old enough yet.")
Not old enough yet.

Example 2: Chained Range Check

score = 85 if 90 <= score <= 100: grade = "A" elif 80 <= score < 90: grade = "B" elif 70 <= score < 80: grade = "C" else: grade = "F" print(grade)

Example 3: Multi-Chain Validation

temp = 72 humidity = 45 # Is it comfortable outside? comfortable = 65 <= temp <= 80 and 30 <= humidity <= 60 print(comfortable) # True

Expansion: (65 <= 72 and 72 <= 80) → True and (30 <= 45 and 45 <= 60) → True. Final: True.

Common Mistakes

✕ Using = instead of ==
if x = 5: causes a SyntaxError. Always use == for equality checks.
✕ Comparing different types
"10" > 5 raises TypeError. Convert first: int("10") > 5.
✕ Confusing < with <=
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?

Question 2 of 5

Which of the following is the correct Pythonic way to check if \(x\) is between 1 and 100 (inclusive)?

Question 3 of 5

What is the output of the following code?

x = 5
print(1 < x < 3)

Question 4 of 5

What does "apple" < "banana" evaluate to?

Question 5 of 5

How many times is the variable \(x\) evaluated in the chained comparison \(a < x < b\)?