Phase 1: Programming Fundamentals I · ~35 minutes · Python · uv
Control Flow — Loops
for doesn't count. It pulls one item at a time from anything you can iterate — which is why it works identically on a string, a list, or a file.
Hiring signal: Recognizes an infinite loop from its cause, not just its symptom, and knows exactly which line to fix
What you will learn
- Distinguish for (iterate over a sequence) from while (repeat while a condition holds)
- Explain why Python's for is iterator-based, not counter-based, and what that means for what you can loop over
- Recognize an infinite loop from its cause and know how to interrupt and fix one
- Predict the exact output of a loop with a boundary condition, including off-by-one slicing
Introduction
Type: Learn Languages: Python Prerequisites: Lesson 02 (Control Flow — Conditionals) Time: ~35 minutes
Objective
Learning objectives
- Distinguish
for (iterate over a sequence) from while (repeat while a condition holds) - Explain why Python's
for is iterator-based, not counter-based, and what that means for what you can loop over - Recognize an infinite loop from its cause and know how to interrupt and fix one
- Predict the exact output of a loop with a boundary condition, including an off-by-one slice
What you're building
A script (process_numbers.py) that:
- Defines a real list of at least 10 numbers
- Uses a
for loop to compute their sum and their count without using the built-in sum() (to practice the accumulator pattern: a running-total variable updated each pass) - Contains a comment showing an off-by-one slicing mistake (like
numbers[0:4] when three items were intended) and the corrected line directly below it - Uses a
while loop for one task where the number of iterations genuinely isn't known in advance (for example: keep doubling a starting value until it exceeds 1000, then report how many doublings it took)
A program hangs after being run — no output changes, no error, nothing. The relevant code is:
n = 10
while n != 0:
n = n - 2
What's the most likely explanation, given this lesson?
This is a genuinely useful trap: n != 0 only stops the loop if execution lands exactly on 0. Starting at 10 and stepping by -2 does land exactly on 0 (10, 8, 6, 4, 2, 0), so this specific loop is correct — but it's one changed starting value away from an infinite loop (start at 9 instead, and n steps 9, 7, 5, 3, 1, -1, -3... forever, since it never equals exactly 0). The lesson here isn't "this code is broken" — it's that != 0 is a fragile way to express "until it reaches zero or below," and <= 0 would be safe against both cases. Diagnosing a hang means asking exactly this question: what condition is being waited on, and is there any path where it's never precisely satisfied?
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy