Training a model is inherently repetitive: process each batch, update weights, repeat for every epoch. Loops are Python’s mechanism for that repetition—the control structure behind every training loop, data preprocessing pipeline, and evaluation sweep.
Volume 02 gradient descent described iterative updates; loops turn “repeat until convergence” into executable code. Before PyTorch’s DataLoader abstracts iteration, you must understand for and while at the Python level.
Learning Objectives
By the end of this lesson, students should be able to:
- Write
forloops to iterate over sequences andrange(). - Write
whileloops with clear termination conditions. - Iterate over datasets represented as lists of features and labels.
- Use
enumerate()andzip()for indexed and parallel iteration. - Apply
breakandcontinuefor early stopping and skipping. - Recognize when vectorized NumPy replaces explicit Python loops.
The for Loop
A for loop iterates over each item in an iterable—a sequence or collection that yields elements one at a time. The loop variable takes each value in turn until the iterable is exhausted.
Iterating Datasets
Before tensors, datasets are often parallel lists or lists of tuples. zip() pairs features with labels row by row.
| Pattern | Syntax | AI Use Case |
|---|---|---|
| Sequence iteration | for x in data |
Process each sample or batch |
| Counted loop | for i in range(n) |
Epoch loops, fixed iterations |
| Indexed iteration | for i, x in enumerate(data) |
Log sample index on error |
| Parallel iteration | for a, b in zip(A, B) |
Pair features with labels |
The while Loop
A while loop repeats as long as its condition is True. Use it when the number of iterations is not known in advance—for example, training until loss falls below a threshold.
for Loop
- Known iteration count or finite iterable
- Epoch loops over
range(num_epochs) - Iterate batches from a dataset
- Preferred default in Python
while Loop
- Condition-driven termination
- Train until loss < threshold
- Risk of infinite loops if condition never false
- Always ensure progress toward exit
break, continue, and Nested Loops
break exits the innermost loop immediately—useful for early stopping. continue skips to the next iteration—useful for filtering invalid samples.
train_epoch() and evaluate().Loops vs Vectorization
Explicit Python loops over millions of samples are slow. NumPy and PyTorch replace inner loops with vectorized operations on arrays. Learn loops first to understand logic; reach for vectorization for performance.
Python Loop
- Clear, readable control flow
- Fine for small data and prototyping
- Slow on large datasets
- Essential for I/O and API calls
Vectorized (NumPy)
- Operations on entire arrays
- 10–100× faster on large data
- Module 3.3 covers in depth
- Training still loops over epochs/batches
Common Misconceptions
for epoch in range(10) gives epochs 1–10.”
Why people believe it: Human counting often starts at 1.
Reality: range(10) yields 0 through 9. Use range(1, 11) for 1-based epoch labels in logs.
break exits all nested loops.”
Why people believe it: Desired behavior in early stopping.
Reality: break exits only the innermost loop. Use a flag variable or refactor into a function with return to stop outer loops.
Why people believe it: Vectorization is emphasized heavily.
Reality: Training loops over epochs and batches are universal. Only the inner per-element math is vectorized.
Quick Knowledge Check
- Short Answer: What does
range(3)produce? Answer: 0, 1, 2. - True/False:
zip(features, labels)pairs elements by position. Answer: True. - Multiple Choice: Best loop when epoch count is fixed: (a) while, (b) for, (c) if, (d) def. Answer: (b).
- Short Answer: What does
breakdo? Answer: Exits the innermost loop immediately. - True/False:
enumerate(data)yields (index, item) pairs. Answer: True. - Short Answer: When prefer
whileoverfor? Answer: When iterations depend on a runtime condition (e.g., loss threshold). - Multiple Choice: Iterating 1M floats in pure Python vs NumPy: (a) Python faster, (b) NumPy faster, (c) same, (d) neither works. Answer: (b).
- True/False:
continueskips the rest of the current iteration. Answer: True. - Short Answer: Write a loop that sums a list of batch losses. Answer: total=0; for L in losses: total += L.
- Multiple Choice: Training loops in PyTorch still use: (a) no loops, (b) epoch/batch loops, (c) only recursion, (d) only while. Answer: (b).
Key Takeaways
forloops iterate iterables;range()drives counted epoch loops.zip()pairs features with labels;enumerate()adds indices.whileloops suit condition-based termination with a safety cap.breakandcontinuecontrol flow for early stopping and filtering.- Vectorize inner math; keep outer epoch/batch loops in Python.
- Next: Functions to structure loop bodies into reusable code.
Hands-on idea: Implement a 5-epoch training simulation with a list of fake batch losses, printing running average each epoch.
Debugging exercise: Remove the epoch < 1000 guard from a while loop and demonstrate an infinite loop—then fix it.
Discussion prompt: In a nested epoch×batch loop, where should early stopping check live?
train_epoch() and other reusable training utilities.