Making decisions
if, elif and else: the program asks a question and follows the first answer that is true.
A question with two answers
Not every line should run every time. An if line asks a question, and the indented lines under it run only when the answer is yes. An else gives the other answer a home, so both paths are written down and neither is an accident.
Read it out loud like a sentence: if the temperature is greater than 18, take a light jacket; otherwise, take a warm coat. The code below is that sentence, written for Python.
if, else, and the advice
temperature = 21
if temperature > 18:
print("Take a light jacket.")
else:
print("Take a warm coat.")The colon and the spaces
Two pieces of punctuation carry the whole idea. The colon at the end of the if line means "a block follows". The four spaces in front of the lines under it are how Python knows they belong to that block rather than to the line after it.
This is not a style choice — Python uses the indentation as grammar. Four spaces is the convention, and consistent spaces matter more than the exact count: a block whose lines are not indented the same is an IndentationError, and the message names the line that lost its way.
Decide what to wear
Write an if/else below the greeting: a light jacket when the temperature is above 18, a warm coat otherwise. Then change the number, run again, and watch the advice follow.
More than two paths: elif
elif means "otherwise, if". It adds another question after the first, and Python walks down the list, running the first block whose question is true and skipping the rest.
Order matters, because the first true question wins. Test the coldest range first if the answers overlap, or a mild question will answer for a freezing day before the freezing one is ever asked.
Asking and assigning look alike
= gives a value a name, and == asks whether two values are the same. One character apart, and it is the most common mistake a beginner makes — writing if temperature = 18: where the question was meant.
When it happens, Python says something about a syntax error rather than answering wrongly, which is its way of being kind. Read the line number, and count the equals signs.
The branch you did not take
An if chooses one path and skips the other entirely. The skipped lines are not run slowly or partially; they are never executed, so a mistake inside them stays hidden until the condition changes. That is why a program can look correct for weeks and fail on the first cold morning: the warm-weather branch was the only one being tested.
When a decision misbehaves, print the condition itself before the branch, not the value you think it used. print(temperature > 18) answers True or False and shows which path the program is about to take. Then print the value. Two prints, one line apart, separate the value from the comparison and show which of the two surprised you.
If the advice never changes, print the value just before the if. The program is only ever asking about the value it actually holds.
Conditions can be combined
A comparison produces a value, True or False, and if simply asks whether that value is true. That is why a condition can be a single comparison or several joined with and, or and not.
and is true only when both sides are; or is true when at least one is; not flips the answer. They bind in that order — not tightest, then and, then or — so when the sentence in your head has a comma in it, write brackets to say which part belongs together.
Not every question needs a comparison
An if does not require == or >. It asks whether the value is truthy: empty text, the number 0, an empty list and None count as false; almost everything else counts as true.
That makes if name: a short way to ask whether text was supplied. It also makes if temperature: a poor test for a real temperature of 0, which is a value like any other — be explicit when zero is meaningful.
Three slips: if temperature > 18 and < 30: is a syntax error because each side of and needs its own comparison; testing age == "18" against the number 18 quietly gives False rather than an error; and an if with no colon, or a body with inconsistent spaces, stops the file before anything runs.
Try these next
- Add a fourth band to the advice — freezing below 0, warm below 12, mild below 20, hot above.
- Rewrite
if not temperature > 18:as a direct comparison, then check both give the same advice. - Print the result of
18 < temperature < 25for three different temperatures.
Three ticket prices
Complete the price ladder: free under 5, half price from 5 to 17, and full price from 18. Run it for ages 3, 12 and 30 by changing the variable.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does if temperature > 18: do?
