558 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Programming in Python · Week 5 — Nested iterations Deep study for Quiz 2 week 5. Nested loops multiply work: outer drives rows, inner completes fully each time.

Programming in Python · Week 5 — Nested iterations
Deep study for Quiz 2 week 5. Nested loops multiply work: outer drives rows, inner completes fully each time.
Week map
Outer loop → inner loop completes per outer step → iteration count product → search flag → running min/max → break scope.
Nested loop notation
- Outer index → often row or first coordinate → runs its full range once per “round.”
- Inner index → completes entire range for each outer value → total body runs = outer count × inner count.
break→ exits innermost loop only → outer continues unless restructured.
Mini-example:
pythoncount = 0 for i in range(3): for j in range(2): count += 1 # count = 6 (3 × 2)
Search pattern
pythonfound = False for item in seq: if item == target: found = True break
Set flag, break on match.
for/else: else runs only if loop did not break.Min/max pattern
pythonbest = seq[0] for x in seq[1:]: if x > best: best = x
Initialize from first element — not 0 when data may be all negative.
Pattern families
Easy — Nested count
Multiply outer and inner trip counts. Trace one outer step showing full inner run. List final values of loop variables.
Medium — Search and index
Linear search with flag or break. Track index with
enumerate or manual counter. Report first match vs all matches.Hard — Min/max with updates
Safe initializer for negatives. Nested search: find max in each row, or best pair sum.
break only leaves inner loop — trace carefully.Worked mini-examples
Example 1 — Grid pairs.
pythonpairs = [] for i in range(2): for j in range(3): pairs.append((i, j)) # 6 pairs: (0,0)..(1,2)
Example 2 — Search.
pythonnums = [4, 7, 2, 9] target = 7 found = False for n in nums: if n == target: found = True break # found is True
Example 3 — Max.
pythondata = [-3, -8, -1, -5] m = data[0] for x in data: if x > m: m = x # m = -1
Example 4 — break scope.
pythonfor i in range(3): for j in range(3): if j == 1: break # inner breaks at j=1; outer continues # outer runs 3 times; inner runs 2 times each → 6 inner bodies
Traps
breakonly leaves inner loop in nested structure.- Max initialized to 0 fails for all-negative data.
- Off-by-one:
range(len)vs direct iteration. - Inner variable shadows outer (
for iinsidefor i). - Counting iterations as
n + minstead ofn × m.
Diagnostic (try yourself)
- How many times does the body run?
pythonfor a in range(4): for b in range(3): pass
- After nested loops below, what is
j?
pythonfor i in range(2): for j in range(5): pass
-
Write a loop that sets
found = Trueand breaks whentargetappears in listitems. -
Find max of
[-10, -3, -7]— why shouldbestnot start at 0? -
In a 3×3 nested loop with
breakwhenj == 0in the inner loop, how many inner bodies execute per outer step?