If, Elif and Else
Run one block or another, depending on what is true. Indentation is not decoration here - it is how Python knows what belongs to the branch.
Only the first matching branch runs
Python checks each condition top to bottom and stops at the first true one. else catches everything left over.
Indentation is the block
The indented lines belong to the branch. The unindented line runs either way - change the temperature and see which lines move.
Reading a branch
if line is required, and the body below it must be indented - four spaces is the convention.elif is "else if". You can have as many as you like; else is optional and there can only be one.pass.If, Elif and Else: A Practical Guide
One path taken, the rest skipped.
Quick Context
An if takes any expression that is true or false and runs the indented block below it only when that expression is true. Everything you learned about comparisons and truthiness feeds straight in here - this is where those values finally do something.
Why order matters
Because only the first matching branch runs, a broad condition placed early will shadow the specific ones after it. If the 70 check came first, a score of 95 would still print C - it would match, and the 90 branch would never be reached. Specific first, general last.
Interactive Exploration Guide
- Run the first editor. A score of 72 prints C. Only one letter appears, even though 72 also satisfies nothing below it.
- Reorder to see shadowing. Move the
score >= 70branch to the top, set the score to 95, and run. It prints C - the wrong answer, produced by correct code in the wrong order. - Run the second editor. Three lines print. Note that this always runs is not indented, so it is outside the branch.
- Indent that line. Put four spaces in front of it and set
temp = 10. It now belongs to the branch, so it disappears from the output - indentation alone changed the meaning.
Key Takeaway
if/elif/else picks exactly one branch: the first whose condition is true. Indentation defines what belongs to that branch, so moving a line in or out by four spaces genuinely changes what the program does. Put specific conditions before general ones, or the general one will shadow them.