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 7.
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 finalprint("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 isFalse. 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 thatscore >= 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 syntaxif, elif, and else line ends with a colon.Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.
Download the Lesson 9 slides (PowerPoint, with narration)
Download the Lesson 9 slides (PDF)
elif: under 13 → "Child", 13 to 19 → "Teen",if line, read the error,The program picks one message depending on the score, using if / elif / else from the lesson. Change the score and run again.
Try this: Change the score to 85 and press Run again — the message becomes “Good job!” Try 95 and 60 too.
Press Run to see the output here.
Save this as lesson09-practice.py and run it with F5 in IDLE.
# 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")
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.
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. One — the first true block (and the else, if none is). 5. Because indentation is how Python decides which lines belong to an if, elif, or else.