hayaliv / Hi! Python / Level 0
Control flow
if chooses a path, for and while repeat work, and break and continue steer a loop.
Think of it like this
Think of a post office sorting table. A clerk checks each letter in turn (for), puts it in one bin depending on its address (if/elif/else), and may stop early when a stop-sign letter arrives (break).
Where the analogy stops being true. A clerk uses judgement, but Python follows your conditions exactly and checks them in order.
What to know
- At most one block of an if/elif/else runs: the first one whose condition is true. If none is true, the else block runs, when there is one.
- A for loop visits the items of a sequence one by one.
- range(n) counts from 0 and stops before n. The end point is never part of it.
- A while loop repeats as long as its condition is true.
- break ends the nearest enclosing loop, and continue jumps to its next round.
- enumerate(items, start=1) gives (count, item) pairs.
- dict.items() gives (key, value) pairs, so a for loop can unpack both.
Example
replies = ["Let's meet", "Not interested", "Send pricing"]
for i, r in enumerate(replies, start=1):
print(i, r)
counts = {"meeting_request": 3, "not_interested": 5, "question": 0}
for label, n in counts.items():
if n >= 5:
print(label, "common")
elif n > 0:
print(label, "rare")
else:
print(label, "none")
total = 0
while total < 3:
total += 1
print("total", total)
for n in range(5):
if n == 1:
continue
if n == 3:
break
print(n)
It prints:
1 Let's meet
2 Not interested
3 Send pricing
meeting_request rare
not_interested common
question none
total 3
0
2
Predict, then check
1. What does this print?
print(list(range(3)))Show the answer
[0, 1, 2]range(3) produces 0, 1 and 2. The end point 3 is left out.
2. What does this print?
for n in range(5):
if n == 2:
continue
if n == 4:
break
print(n)Show the answer
0
1
3n == 2 skips to the next round without printing. At n == 4 the loop stops before printing.
3. What does this print?
n = 0
while n < 3:
n += 2
print(n)Show the answer
4n goes 0, then 2, then 4. At 4 the condition n < 3 is false, so the loop ends and 4 is printed.
4. What does this print?
x = 0
if x > 5:
print("big")
elif x > 3:
print("mid")
print("done")Show the answer
doneNeither condition is true and there is no else, so no block runs. Only the last line prints.
Try it
Write first_meeting(labels) that returns the index of the first 'meeting_request' in the list, or -1 if there is none. Use a loop.
def first_meeting(labels):
return -1
Show a solution
def first_meeting(labels):
for i, label in enumerate(labels):
if label == 'meeting_request':
return i
return -1Common mistake
range(3) never yields 3. Its end point is not part of the sequence.
In a small-language-model project
In a fine-tuning project you usually loop over examples with enumerate to number them, and over a counts dict with items() to print a label report.
Sources
Each quote below appears word for word on the cited page of the official Python documentation (version 3.12).
It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true
docs.python.org/3.12/reference/compound_stmts.html — if/elif/else selects one suite by testing conditions in order.Python's for statement iterates over the items of any sequence
docs.python.org/3.12/tutorial/controlflow.html — A for loop iterates over the items of a sequence.The given end point is never part of the generated sequence
docs.python.org/3.12/tutorial/controlflow.html — The end point of range is not part of the sequence.The while statement is used for repeated execution as long as an expression is true
docs.python.org/3.12/reference/compound_stmts.html — while repeats as long as the expression is true.It terminates the nearest enclosing loop
docs.python.org/3.12/reference/simple_stmts.html — break ends the nearest enclosing loop.It continues with the next cycle of the nearest enclosing loop.
docs.python.org/3.12/reference/simple_stmts.html — continue goes to the next cycle.returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable
docs.python.org/3.12/library/functions.html — enumerate yields a count and a value.Return a new view of the dictionary's items ( (key, value) pairs).
docs.python.org/3.12/library/stdtypes.html — dict.items() gives key-value pairs.If all expressions are false, the suite of the else clause, if present, is executed.
docs.python.org/3.12/reference/compound_stmts.html — The else suite runs when all conditions are false.