Comparison Operators
Every decision in your future code — every if, every filter, every login check — begins with one of these six operators asking a True/False question.
The six questions
age = 20
print(age == 20) # True equal to
print(age != 18) # True not equal to
print(age > 18) # True greater than
print(age < 18) # False less than
print(age >= 20) # True greater than or equal
print(age <= 19) # False less than or equal
Each comparison returns a boolean — a value you can store, pass around, and print:
can_vote = age >= 18
print(can_vote) # True
print(type(can_vote)) # <class 'bool'>
That’s a big deal: comparisons aren’t just for if statements — they produce values. You’ll store them, put them in variables, and sum them up later in the course.
The = vs == rule (final exam version)
score = 85 # assignment: score BECOMES 85
print(score == 85) # comparison: ASKS if score equals 85 → True
Inside if statements, using = instead of == is a syntax error in Python (other languages silently allow it — Python protects you):
if score = 85: # SyntaxError: invalid syntax. Did you mean '=='?
Read the error text — modern Python literally suggests the fix.
Chaining comparisons — a Python superpower
Math-class notation works directly:
age = 25
print(18 <= age <= 65) # True — "between 18 and 65, inclusive"
# equivalent to the longer form:
print(age >= 18 and age <= 65)
0 < x < 100 reads exactly like math class — and it’s not a trick, it’s real Python. Use it; it’s cleaner.
Comparing strings
print("apple" == "apple") # True
print("Apple" == "apple") # False — case matters!
print("apple" < "banana") # True — dictionary order
print("" == "") # True
Two practical patterns:
# Case-insensitive check — normalize first
answer = input("Continue? (yes/no) ")
print(answer.lower() == "yes")
# Empty-string check
name = input("Name: ")
print(name == "") # did they type nothing?
Comparing different types
print(5 == 5.0) # True — int and float compare by VALUE
print("5" == 5) # False — string vs number: never equal
print("5" > 3) # TypeError: '>' not supported between 'str' and 'int'
== between mismatched types is simply False (no crash). But <, > between incompatible types crash. If you hit that TypeError, something upstream gave you a string where you expected a number — convert it.
Comparing with None — the is habit
result = None
print(result == None) # works, but...
print(result is None) # ← the Pythonic way
Both work, but is None is the community standard for checking “is there no value?” You’ll meet is formally in the operators module’s identity lesson.
Common Errors & Fixes
SyntaxErrorwith=inside if — use==.- Comparison always False — string vs number, or case mismatch. Print both sides and their
type()s. TypeError: '>' not supported— incompatible types; convert before comparing.
✅ Checkpoint
- What does
10 != 10return? (False) - Write a chained check: is
xbetween 1 and 10? (1 <= x <= 10) "Yes" == "yes"? (False — normalize with .lower() first)- Which do you use to check for None? (is None)
Next: logical operators — combining your True/False values into real decisions.