Booleans and Comparison Operators
Every decision your code will ever make — every if, every loop, every button — boils down to one tiny type with only two possible values: True and False. Understand booleans and control flow becomes easy next module.
The type with two values
is_raining = True
is_sunny = False
print(type(is_raining)) # <class 'bool'>True and False are keywords — capital first letter, and they're not strings. "True" (in quotes) is just text; True is the actual boolean value.
Comparison operators produce booleans
You rarely write True by hand — you *generate* booleans by comparing things:
score = 85
print(score == 100) # False (equal to — TWO equals signs!)
print(score != 100) # True (not equal)
print(score > 80) # True (greater than)
print(score >= 85) # True (greater or equal)
print(score < 80) # False
print(score <= 85) # TrueThe single most important = vs == lesson
This is the #1 beginner confusion in ALL of programming, so let's kill it now:
| Symbol | Name | Meaning | Example |
|---|---|---|---|
= | assignment | "put the right side into the left" | score = 85 |
== | comparison | "is the left side equal to the right?" | score == 85 → True |
One equals stores. Two equals asks. Say it out loud until it's automatic.
Comparing strings works too
print("apple" == "apple") # True
print("Apple" == "apple") # False! — capitalization matters
print("b" > "a") # True — alphabetical orderThat second one causes real bugs: "Yes" == "yes" is False. When comparing user input, normalize first: answer.lower() == "yes".
Combining booleans: and, or, not
age = 20
has_id = True
print(age >= 18 and has_id) # True — BOTH must be true
print(age < 18 or has_id) # True — at least ONE true
print(not has_id) # False — flips the value| and | or | not |
|---|---|---|
| True only if both True | True if at least one True | Flips the value |
Real-world shape: if age >= 18 and has_ticket: — "let them in only if both hold."
Booleans are secretly numbers
A fun true fact: True is 1 and False is 0:
print(True + True) # 2
print(False * 100) # 0Which enables an elegant trick — counting matches:
answers = [True, False, True, True]
print(sum(answers)) # 3 — True counts as 1!Common Errors & Fixes
==."5" == 5 is False. Convert types first."apple" > 5 confuses Python. Compare like with like.✅ Checkpoint
= and ==? *(Assignment vs comparison)*5 != 5 print? *(False — "not equal" is false when they ARE equal)*"Yes" == "yes"? *(False — case matters)*True and False? True or False? not True? *(False, True, False)*Next: type conversion — the lesson that explains the input() trap you've been warned about twice already.