Booleans, comparisons, and if/else
Make decisions with True/False, comparisons, and if / elif / else — and understand truthiness.
Why this matters
All the arithmetic in the world is useless if a program cannot make a decision. True/false questions are everywhere: "Is the user signed in?", "Is the price over 100?", "Did the file end?". Booleans are Python's way of representing true/false, and if statements are how you make your program take different paths based on them. This is the moment programs stop being recipes and start being logic.
Learning objectives
After this lesson you will be able to:
- Create and understand
True/False. - Compare values with
==,!=,<,>,<=,>=. - Combine conditions with
and,or,not. - Write an
if/elif/elseblock that takes different paths. - Understand "truthiness" — how values behave as true/false even when they are not booleans.
1. Booleans — True and False
A boolean is a value that is either True or False. Note the capital letters — Python is case-sensitive.
is_running = True
is_done = False
print(is_running) # True
print(is_done) # FalseYou usually do not write True / False directly. You compute them with a comparison.
2. Comparisons — asking questions that answer true/false
These operators return a boolean:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal | 5 == 5 | True |
!= | not equal | 5 != 3 | True |
< | less than | 3 < 5 | True |
> | greater than | 5 > 3 | True |
<= | less or equal | 5 <= 5 | True |
>= | greater or equal | 5 >= 9 | False |
age = 30
age == 30 # True
age != 30 # False
age > 18 # True
age <= 21 # FalseThe classic beginner trap: = vs ==
=assigns:x = 5puts 5 into x.==compares:x == 5asks "is x equal to 5?" and returns a boolean.
if score = 100: # ERROR — this assigns, and is not allowed in an if
if score == 100: # OK — this asks "is score equal to 100?"3. Combining conditions: and, or, not
You can combine boolean values into bigger questions.
and— both must be true.or— at least one must be true.not— flips true to false and vice versa.
age = 30
has_id = True
age > 18 and has_id # True and True -> True
age > 18 or has_id # True
not has_id # Falsescore = 85
if score >= 80 and score <= 100:
print("Great score!")You can chain comparisons directly in Python:
if 80 <= score <= 100:
print("Great score!")This reads naturally and is the Pythonic way.
4. if / elif / else — making a choice
The if statement runs a block of code only when its condition is True.
age = 20
if age >= 18:
print("You are an adult.")else — the other path
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are not yet an adult.")elif — more than two paths
"elif" is short for "else if". Run the first condition that is true; skip the rest.
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(grade) # BPython checks each if/elif top to bottom and takes the first one that is true. Order matters — notice we check >= 90 before >= 80, or an 88 would wrongly match >= 80 only after we've already ruled out the higher grade.
Indentation is the grammar
In Python, the indentation (4 spaces) at the start of a line defines which block a statement belongs to. It is not optional. Whatever is indented under the if runs when the condition is true.
if age >= 18:
print("adult") # indented: runs only if the condition is true
print("welcome") # also runs only if true
print("done") # NOT indented: always runs5. Truthiness — values that behave like True/False
This is a Python quirk that surprises newcomers but is very handy. When you put a non-boolean value in an if, Python treats some values as "falsey" and everything else as "truthy".
The falsey values are:
FalseNone0and0.0""(empty string)[],{},set()(empty collection)
Everything else is truthy.
name = "Ada"
if name:
print("The name is not empty")
else:
print("The name is empty")Here name is non-empty, so it is truthy and we print the first line. If name were "", it would be falsey.
This lets you write short, readable checks. But be aware of it — a common bug is checking if x: when you meant to test whether x equals a specific value.
Common mistakes
=instead of==in anif— the classic. Remember==asks a question.- Forgetting the colon at the end of the
if/elseline. - Inconsistent indentation — mixing tabs and spaces, or varying the number of spaces. Use 4 spaces everywhere. Your editor should do this.
- Wrong
eliforder — check the most specific case first. andvsormix-up —andrequires both;orneeds just one. Be explicit when you write it out loud.
Hands-on lab (45 minutes)
- Write an
ifthat prints "big" if a number is over 100, else "small". - Given
ageandhas_license, print "can drive" only ifage >= 18ANDhas_licenseis true. - Write a
gradefunction snippet that maps a score to A/B/C/F usingelif. Test it with 95, 85, 75, and 50. - Print "even" if a number is even (
n % 2 == 0) else "odd". - Use an f-string inside an
elsebranch: print a message that includes one of your variables. - Challenge: a username is valid if it has at least 3 characters and contains no spaces. Write the check and print "valid" / "invalid".
Self-check
- What is the difference between
=and==? - What does
True and Falseevaluate to?True or False? - Which two values are "falsey" besides
False? - Why does order matter in an
if/elifchain? - What does
if name:check whennameis a string?
References
Sign in to save your progress and earn badges.