1.2.15 · D1Introduction to Programming (Python)

Foundations — if - elif - else — syntax, indentation rules

2,133 words10 min readBack to topic

Before you can read if score >= 90: you must already know six tiny things: what a value is, what a variable is, what True/False mean, what a comparison like >= does, what a : colon promises, and what indentation ties together. Then we meet the three keywords themselves — if, elif, else — and see exactly what each does. We build each one from nothing, in the order they stack.


1. A value — the thing your program holds

Why the topic needs it: an if decides based on what data you have. No values → nothing to decide about.


2. A variable — a labelled box for a value

Look at Figure s01: the box on the left has a name taped to it (marks), and inside sits the value 72. The = is the act of putting the card in the box — it is not a question.

Why the topic needs it: conditions like marks >= 40 read the value out of a box. Without variables there'd be nothing to compare.


3. Booleans — the two answer-cards True and False

Why this tool and not a number? Because a decision is binary — you either take the umbrella or you don't. A boolean is the smallest possible value that can express "yes" versus "no", which is precisely what a branch needs. See Booleans and comparison operators and Truthiness in Python for how other values (like 0 or "") get read as False.


4. Comparison operators — the machines that produce a boolean

Look at Figure s02: the operator >= is a little machine. You feed in 72 and 40; it weighs them and drops out a single answer-card — here True.

Why the topic needs it: if cannot ask its own question — the comparison operator builds the boolean, and if merely reacts to it. Chain several with Logical operators and, or, not when one test isn't enough.


5. The colon : — a promise that a block follows

Why this tool: Python needs one clear signal that says "a group of dependent lines starts now". The colon is that signal — cheap, visible, and it appears on every header (if, elif, else, and later while/for).


6. Indentation — the grammar that groups lines

Look at Figure s03: the if header opens a door with :. The two lines pushed 4 spaces to the right are inside the door. The next line pulled back to the left is outside — it runs no matter what the question answered.

Why the topic needs it: braces {} don't exist here. Indentation is the only thing tying a set of actions to a particular door — it is the heart of if / elif / else.


7. The three keywords themselves — if, elif, else

Now every tool is in place. The keywords are the doors that use those tools. Look at Figure s04: three doors in a hallway, walked in order, and you leave through exactly one.


A one-look worked trace (ifelse)


A multi-way trace (ifelifelse)


Equipment checklist

Test yourself — cover the right side, answer, then reveal.

What does marks = 72 do, in plain words?
Puts the value 72 into the box named marks (assignment, not a question).
What are the only two boolean values?
True and False.
What does the operator >= hand back?
A boolean — True or False.
Read == aloud.
"is equal to?" — it asks a question and returns a boolean.
Which symbol assigns and which compares?
= assigns (fills a box); == compares (asks a question).
What does the colon : at the end of an if line promise?
That an indented block follows and belongs to that header.
How does Python know which lines are inside a block?
By their indentation (leading whitespace), not by braces.
Standard number of spaces per indent level?
4 spaces.
Why can't you mix tabs and spaces?
Python treats them as different characters → TabError.
What does the if keyword do when its condition is True?
Runs its indented block and skips the rest of the chain.
When does an elif block run?
Only when every condition above it was False and its own condition is True.
Can an elif appear without an if above it?
No — an elif may only follow an if or another elif, else SyntaxError.
When does the else block run, and how many are allowed?
When all preceding conditions were False; at most one else, at the end, and it's optional.
In a full if/elif/elif/else chain, how many blocks run?
Exactly one — the first True condition, or else if none are True.

Connections

  • Parent: if / elif / else
  • Booleans and comparison operators — where True/False and >= come from
  • Logical operators and, or, not — combining several conditions
  • Truthiness in Python — non-boolean values inside if
  • Indentation and code blocks — the grammar in full
  • while and for loops · match-case statement · Ternary conditional expression — reuse the same tools