Command Palette

Search for a command to run...

Hectal
Phase 2Beginner3 of 18 in Python

Control Flow

Make programs decide with if and match, repeat with while and for, and learn the loop tools every Python programmer uses: range, break, enumerate, zip and :=.

So far every program you wrote ran straight from the first line to the last. Real programs need two more powers: deciding (do this only if that is true) and repeating (do this again and again until a goal is reached). Together they are called control flow, because they control which lines run and how many times.

This phase teaches decisions with if, elif, else, the one-line conditional expression and the modern match statement. It teaches repetition with while and for, the helpers that make loops short and safe (range, enumerate, zip, reversed, sorted), and the keywords that steer a loop (break, continue and the loop else).

You will also meet the walrus operator :=. Every topic has examples you can run and edit here. Lists appear in many loop examples before Phase 4 teaches them properly: for now, a list such as [3, 8, 5] is just several values kept in order, and for can visit them one by one. Functions arrive in Phase 5, so none are used here.

0/9 · 0%
9 topics ~4 h 22 code blocks & diagrams
Start with the first topic
1
2.1

Making Decisions: if, elif, else

if runs a block of code only when a condition is True; elif adds more conditions to try in order, and else catches everything left. Python picks at most one branch of an if/elif/else chain, and the indentation shows which lines belong to it.

31 min 1 diagram 2 code practice

2
2.2

The One-Line if (Conditional Expression)

The conditional expression a if condition else b picks one of two values in a single line. It is perfect for choosing a value, and the or default trick is shorter still but fails when a valid value is falsy, such as 0.

26 min 2 code practice

3
2.3

Repeating with while

A while loop repeats a block of code for as long as its condition stays True. You must make something inside the loop change the condition, or the loop never ends.

27 min 3 code practice

4
2.4

for Loops and range()

A for loop visits each item of a sequence one by one, and range(start, stop, step) produces the numbers for counting loops. range stops before stop and is lazy: it never builds the whole list in memory.

31 min 1 diagram 2 code practice

5
2.5

break, continue and the Loop else

break leaves a loop at once, continue skips the rest of the current pass, and the unusual else on a loop runs only when the loop finished without a break. Together they express searching and early exit cleanly.

25 min 2 code practice

6
2.6

Looping Like a Pythonista: enumerate and zip

enumerate gives you the position and the item together, zip walks several sequences in step, and reversed and sorted change the order. They replace the beginner pattern for i in range(len(x)).

24 min 2 code practice

7
2.7

Nested Loops and Patterns

A loop inside a loop runs the inner loop completely for every pass of the outer loop, so the total work is rows times columns. Nested loops draw grids and tables, and leaving both loops at once needs a flag, a for/else, or (later) a function.

30 min 2 code practice

8
2.8

Structural Pattern Matching: match

match compares a value against a series of patterns and runs the first case that fits. It can test exact values, alternatives, the shape of lists and dictionaries, and capture parts of what it matched into variables. It is not a C-style switch: a bare name in a case captures, it does not compare.

34 min 1 diagram 2 code practice

9
2.9

The Walrus Operator :=

The walrus operator := assigns a value to a name inside an expression, so you can test a value and keep it in one step. It shortens loops that read until a stop signal and filters that need a computed value twice, and it is not worth using when it makes a line harder to read.

26 min 2 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.