Skip to chapter content
navan✳THE BUILD LABPYTHON / GUIDE
0/26 read
CHAPTER 02 / 26 · FOUNDATIONS

Decisions and flow control.

Use a condition to choose one path, then make every branch understandable.

BY NAVAN SAMI5 MIN READTHEORY FIRST
01 / THE BASICS

Start here.

A program does not always do every possible action. A condition chooses which instructions run. A comparison such as score >= 50 asks whether score is greater than or equal to 50 and produces either True or False. Those are Boolean values, Python's two answers to a yes-or-no question. Other comparison signs include <, >, <=, == for equality, and != for inequality. One equals sign assigns; two equals signs compare.

An if statement begins with if, a condition, and a colon. The lines indented underneath are its body. Python evaluates the condition; if it is True, that body runs. elif means 'else if' and is checked only when earlier conditions failed. else has no condition and catches everything that remains. Only the chosen branch of one if/elif/else chain runs. After the chain, code at the original indentation level continues normally.

The example checks the highest grade first. With score equal to 72, score >= 80 is False, but score >= 50 is True, so grade receives 'Pass'. For a score of 80, the first branch wins and the later branches are skipped. This order is part of the program's policy. Before writing a condition, state the boundary in ordinary words: does 50 pass, and does 80 belong to the highest grade? Precise comparisons make those answers visible.

02 / THE IDEA

Build the model.

A decision turns data into a route through a program. An if statement evaluates a condition, runs its indented body when the condition is true, and skips it otherwise. elif adds another condition that is checked only if earlier branches did not run. else handles everything left. This lets a program explain its policy in an order a reader can follow.

A comparison such as score >= 50 produces a Boolean value, True or False. and and or combine decisions, while not reverses one. They short circuit: if the left side already determines the result, Python does not evaluate the right side. That can be useful for guarding a value before reading one of its properties.

03 / UNDER THE SURFACE

How it works.

Indentation is part of Python's syntax, not decoration. Lines at the same indentation level belong to the same block. When one branch finishes, execution continues after the whole if/elif/else statement. Before writing code, state the cases in ordinary language, including boundary values. Then order conditions so the cases do not accidentally overlap.

Truthiness can shorten conditions, but it can also hide intent. An empty list, empty string, zero, and None are falsy. If zero is a valid score, test score is None for absence instead of if not score. Use explicit comparisons when a reader needs to understand the exact boundary that sends a value down one route.

04 / A WORKED EXAMPLE

Read it line by line.

example.py
score = 72
if score >= 80:
    grade = 'A'
elif score >= 50:
    grade = 'Pass'
else:
    grade = 'Retry'
print(grade)

The first comparison is false because 72 is below 80. Python moves to the elif condition, which is true, so it assigns 'Pass'. The else body is skipped. The final print runs after the conditional and displays Pass. At a score of exactly 80, the first branch would run instead.

05 / ANOTHER EXAMPLE

See the rule again.

example-2.py
temperature = 18
if temperature < 10:
    label = 'cold'
elif temperature < 25:
    label = 'mild'
else:
    label = 'hot'
print(label)

The output is mild. The second condition does not need to repeat temperature >= 10 because reaching elif already means the first condition was false. At 9 the result is cold; 10 and 24 are mild; 25 is hot. These boundary values show exactly where each branch begins and ends.

06 / REMEMBER THIS

A mistake to avoid.

Do not write several independent if statements when the cases are mutually exclusive. More than one of them may run and overwrite a result; an if/elif/else chain states that only one route should win.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗Python · Understand the idea, then write the code.