← Master Index
Vol. 03 Module 3.1 Lecture

Loops

Python Basics

How This Lesson Fits the Module

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 for loops to iterate over sequences and range().
  • Write while loops with clear termination conditions.
  • Iterate over datasets represented as lists of features and labels.
  • Use enumerate() and zip() for indexed and parallel iteration.
  • Apply break and continue for early stopping and skipping.
  • Recognize when vectorized NumPy replaces explicit Python loops.

The for Loop

Definition — 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.

# Iterate over a list of per-epoch losses epoch_losses = [0.82, 0.61, 0.45, 0.38, 0.35] for loss in epoch_losses: print(f"loss={loss:.2f}") # range() for counted loops — epochs for epoch in range(5): # 0, 1, 2, 3, 4 print(f"Starting epoch {epoch}")

Iterating Datasets

Before tensors, datasets are often parallel lists or lists of tuples. zip() pairs features with labels row by row.

# Tiny tabular dataset: house size → price features = [800, 1200, 1500, 2000] # sq ft labels = [150000, 220000, 275000, 350000] total_error = 0.0 weight = 200.0 # dollars per sq ft (naive model) for size, price in zip(features, labels): prediction = weight * size error = abs(price - prediction) total_error += error mae = total_error / len(features) print(f"MAE = ${mae:,.0f}")
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

Definition — 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.

loss = 1.0 learning_rate = 0.1 epoch = 0 target_loss = 0.01 while loss > target_loss and epoch < 1000: loss = loss * (1 - learning_rate) # simulated decay epoch += 1 print(f"Converged at epoch {epoch}, loss={loss:.4f}")

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.

# Nested: epochs × batches num_epochs = 3 batch_losses = [0.5, 0.4, 0.35, 0.3] for epoch in range(num_epochs): for batch_idx, batch_loss in enumerate(batch_losses): if batch_loss < 0.32: print(f"Early stop at epoch {epoch}, batch {batch_idx}") break print(f" batch {batch_idx}: loss={batch_loss}")
What’s Next in This ModuleThe next lecture, Functions, packages loop bodies into reusable, testable units like 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

Misconception 1: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.

Misconception 2: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.

Misconception 3: “Real ML code never uses Python 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

  1. Short Answer: What does range(3) produce? Answer: 0, 1, 2.
  2. True/False: zip(features, labels) pairs elements by position. Answer: True.
  3. Multiple Choice: Best loop when epoch count is fixed: (a) while, (b) for, (c) if, (d) def. Answer: (b).
  4. Short Answer: What does break do? Answer: Exits the innermost loop immediately.
  5. True/False: enumerate(data) yields (index, item) pairs. Answer: True.
  6. Short Answer: When prefer while over for? Answer: When iterations depend on a runtime condition (e.g., loss threshold).
  7. Multiple Choice: Iterating 1M floats in pure Python vs NumPy: (a) Python faster, (b) NumPy faster, (c) same, (d) neither works. Answer: (b).
  8. True/False: continue skips the rest of the current iteration. Answer: True.
  9. Short Answer: Write a loop that sums a list of batch losses. Answer: total=0; for L in losses: total += L.
  10. 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

  • for loops iterate iterables; range() drives counted epoch loops.
  • zip() pairs features with labels; enumerate() adds indices.
  • while loops suit condition-based termination with a safety cap.
  • break and continue control 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.
Trainer’s Guide

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?

What’s Next Continue to Functions to define train_epoch() and other reusable training utilities.