Python Fundamentals · Units 1–2

Control Flow

if, loops, range(), break and continue

Unit 2 · Control flow

Make a program choose a path and repeat work

Control flow is the order in which Python follows instructions. Conditions act like a decision at a school gate; loops repeat a job for each student or until a condition changes.

Classroom analogy: an if statement checks a rule before choosing a path. A loop checks a class list or counter so you do not have to write the same instruction again and again.
Worked example · class results
Starting up Python…
marks = [82, 35, 91]
for mark in marks:
    if mark >= 40:
        print(mark, "→ pass")
    else:
        print(mark, "→ retry")
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Interactive line-by-line trace

Follow the program as it runs

Select a decision or loop, then advance one instruction at a time and watch the variables change.

1marks = [82, 35, 91]2for mark in marks:3    if mark >= 40:4        print(mark, "→ pass")5    else:6        print(mark, "→ retry")

Current instruction · 1 / 11

Store the class marks in a list.

Variables and result

marks → [82, 35, 91]

marks list created

See exactly what range() creates

The stop number is not included. A positive step counts upward; a negative step counts downward.

list(range(-2, 8, 2))=[-2, 0, 2, 4, 6]

Choose how the flow changes

break

Exit the nearest loop early, for example when the searched student is found.

continue

Skip the rest of this turn and check the next item in the loop.

pass

Do nothing for now; it keeps an unfinished block syntactically valid.

return

Leave a function and optionally send its result back to the caller.

Now you try

Edit the score and add an else branch that prints “Retry” when the score is below 40.

Your pass-or-retry decision
Starting up Python…
# Task: print Pass for 40 or above, otherwise print Retry.
score = 35
if score >= 40:
    print("Pass")
# Add the else branch here
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Common mistakes

Python uses indentation to show which instructions belong inside a block

IndentationError: expected an indented block

After a line ending in a colon, indent the instructions that belong to it.

The spaces at the start of a line do not match Python's block structure.

SyntaxError: invalid syntax

A missing colon after if, for, or while can stop Python understanding the statement.

Control-flow check

1. What does range(2, 6) produce?

2. When should you use a while loop?

The trace uses the same scores shown in the working Python example.