if runs code only when a condition is trueelse covers the "otherwise" caseelif chains several choicesif mistakesEverything so far has run straight from top to bottom. An if statement breaks that: it runs some code only when a condition is true, and skips it otherwise. The condition is just a comparison, like the ones from Lesson 8.
age = 12
if age >= 10:
print("You are 10 or older.")
print("Done.")
Output:
You are 10 or older.
Done.
Three things to notice. First, the line ends with a colon (:) —
it says "here comes the code for this case." Second, the print
underneath is indented (pushed in, usually four spaces). The indent
is how Python knows that line belongs to the if. Third, the final
print("Done.") is not indented, so it runs no matter what.
Change age to 8 and run it again:
age = 8
if age >= 10:
print("You are 10 or older.")
print("Done.")
Output:
Done.
The indented line is skipped entirely, because the condition is
False. The unindented line still runs. That is the whole mechanism:
condition true → run the indented block; condition false → skip it.
else covers the case where the condition is false:
score = 45
if score >= 50:
print("You passed!")
else:
print("Keep practicing — you will get there.")
Output:
Keep practicing — you will get there.
else needs no condition of its own — it means "in every other case."
And like if, it ends with a colon and its code is indented.
When there are more than two cases, elif (short for "else if")
chains them in order. Python checks each condition from the top and
runs the first block whose condition is true — and only that one:
score = 85
if score >= 90:
print("Excellent!")
elif score >= 70:
print("Good job!")
elif score >= 50:
print("You passed.")
else:
print("Keep practicing.")
Output:
Good job!
score >= 90 is false, so Python moves on. score >= 70 is true, so
it prints "Good job!" and stops checking — it does not matter that
score >= 50 would also have been true. Order matters: always put
the strictest condition first, or a looser one above it will swallow
every case below.
In most languages, indentation is just tidiness. In Python, it is meaning. These two programs do different things:
age = 15
if age >= 10:
print("Old enough.")
print("Welcome in!")
print("Goodbye.")
Output:
Old enough.
Welcome in!
Goodbye.
age = 15
if age >= 10:
print("Old enough.")
print("Welcome in!")
print("Goodbye.")
Output:
Old enough.
Welcome in!
Goodbye.
Wait — those outputs are the same. Now try age = 8 in each. In the
first, both indented lines are skipped, so only "Goodbye." prints. In
the second, only "Old enough." is skipped, and "Welcome in!" still
prints. Same lines, different indentation, different program. Always
indent every line that belongs to the if, elif, or else above
it — and keep the indent consistent (four spaces is the standard).
if age >= 10 without : gives a syntax
error. Every if, elif, and else line ends with a colon.elif: under 13 → "Child", 13 to 19 → "Teen",
20 or older → "Adult". Test it with three different ages.if line, read the error,
then put the colon back and confirm the program works.if statement do?if?else mean, and does it take a condition?if/elif/else chain, how many blocks run?Answer key: 1. It runs code only when its condition is true, and skips it otherwise. 2. A colon at the end of the if line, and indentation on the lines below. 3. "In every other case" — it takes no condition of its own. 4. At most one: the first block whose condition is true (or the else, if none is). 5. Because indentation is how Python decides which lines belong to an if, elif, or else.
if runs its indented block only when its condition is true.else covers every other case, with no condition of its own.elif chains choices in order; the first true condition wins, and only it runs.if/elif/else line ends with a colon; its block is indented four spaces.Next lesson: Loops.
Work through the slides with Helio's narration, or download them:
Open the slidesDownload PowerPointDownload PDF
Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).
Download lesson09-practice.pyDownload the solution
# Lesson 9: If Statements — Making Decisions
# WHAT TO DO: An if statement runs its indented lines only when its
# condition is true. Age 15 should count as a teen (13 or older), but
# one line is wrong. Fix line 10: change 20 to 13 in the if condition.
# HINT: Only change the number 20. The indented print lines are already
# right, and else covers every other case.
# Run with F5 in IDLE. You are done when it prints "You are a teen".
age = 15
if age >= 20: # <-- CHANGE THIS LINE: a teen is 13 or older
print("You are a teen")
else:
print("You are not a teen yet")
Save the file as lesson09-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.
Five quick questions on this lesson. Take the Lesson 9 quiz
Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.