Modules/Python/ Making Decisions

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

The colon at the end of the 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.
Only the first true branch runs. Once one matches, the rest are skipped entirely - so order your conditions from most specific to least.
A block with nothing in it is a SyntaxError. If you want a branch that does nothing yet, write 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

  1. Run the first editor. A score of 72 prints C. Only one letter appears, even though 72 also satisfies nothing below it.
  2. Reorder to see shadowing. Move the score >= 70 branch to the top, set the score to 95, and run. It prints C - the wrong answer, produced by correct code in the wrong order.
  3. Run the second editor. Three lines print. Note that this always runs is not indented, so it is outside the branch.
  4. 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.

Check yourself

0 of 3

Answer without scrolling back up.

  1. score = 95, and the branches check >= 70, then >= 80, then >= 90 in that order. What prints?

  2. What decides which lines belong to an if branch?

  3. How many else clauses can one if statement have?

Cheat sheet

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.

PYTHON · vizlearn.in/python/if_elif_else.html