Quiz 2

Programming in Python · Week 5 — Iterations continued

1134 words
6 min read
2026-08-16T00:00:00.000Z
Python Week 1: the first filter for runtime behavior
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

nested loops, search, min/max patterns — concepts, pattern families, and traps for Quiz 2 week 5. # Week 5 — iterations continued > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

Week 5 — iterations continued

Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.
Part of the Quiz 2 prep system%20%C2%B7%20%5BWeeks%201%E2%80%938%20index%5D(.%2Fmay-2026-python-quiz-2-weeks-1-8-prep) · Pattern atlas · Formula chains.

Week map

Outer loop → inner loop → search flag → running min/max

Classify → Represent → Execute → Trap-check

  • Recognize: Ask: How many times runs inner if outer is 3 and inner is 4?
  • Procedure: Multiply outer and inner trip counts. Trace one outer step showing full inner run. Useful for tables and coordinate pairs.
  • Variations / traps: Watch for: break only leaves inner loop in nested structure.

Formula chain (compressed)

outer×inner loops → O(n²) scans → search flag → min/max over grid.
  1. Nested forfor i in ...: for j in ...: — pairs / grid traversal
  2. Iteration countouter × inner body runs — complexity estimate
  3. Search flagfound = False; break inner — stop early on match
  4. Min/max scanif x < best: best = x — linear search
  5. Indentationbody belongs to nearest for/while — trace nested blocks

Deep study

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:
python
count = 0
for i in range(3):
    for j in range(2):
        count += 1
# count = 6  (3 × 2)

Search pattern

python
found = 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

python
best = 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.
python
pairs = []
for i in range(2):
    for j in range(3):
        pairs.append((i, j))
# 6 pairs: (0,0)..(1,2)
Example 2 — Search.
python
nums = [4, 7, 2, 9]
target = 7
found = False
for n in nums:
    if n == target:
        found = True
        break
# found is True
Example 3 — Max.
python
data = [-3, -8, -1, -5]
m = data[0]
for x in data:
    if x > m:
        m = x
# m = -1
Example 4 — break scope.
python
for 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

  • break only 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 i inside for i).
  • Counting iterations as n + m instead of n × m.

Diagnostic (try yourself)

  1. How many times does the body run?
python
for a in range(4):
    for b in range(3):
        pass
  1. After nested loops below, what is j?
python
for i in range(2):
    for j in range(5):
        pass
  1. Write a loop that sets found = True and breaks when target appears in list items.
  2. Find max of [-10, -3, -7] — why should best not start at 0?
  3. In a 3×3 nested loop with break when j == 0 in the inner loop, how many inner bodies execute per outer step?

ChatGPT prep archive

Archived import for extra depth — complements the notes above, not official IITM material.

Core concepts

  • Nested loops: inner completes fully for each outer step; total iterations = product of counts.
  • Search: boolean found flag or early break when target located.
  • Min/max: initialize with first element or float('inf'); update when comparison holds.
  • break exits innermost loop only unless labeled logic restructures.

Notation & vocabulary

PatternTemplate
Searchflag + break on match
Maxbest = seq[0] then compare
Nestedouter drives rows, inner columns

Pattern families

Easy — Nested count

Multiply outer and inner trip counts. Trace one outer step showing full inner run. Useful for tables and coordinate pairs.

Medium — Find in sequence

Linear search: compare each element. Set found True and break, or use for-else idiom. Report index if needed by tracking i.

Hard — Min with updates

Choose safe initializer. Update when current element beats best. For max of negatives, do not start at 0 unless 0 is in domain.
Drill these on the pattern atlas — filter to week 5.

Traps

  • break only leaves inner loop in nested structure.
  • Max initialized to 0 fails for all-negative data.
  • Off-by-one when using range(len) vs direct iteration.
  • Variable reused name in inner loop shadows outer.

Retrieval prompts

  1. How many times runs inner if outer is 3 and inner is 4?
  2. Why initialize max carefully for negative inputs?
  3. What does break do inside nested loops?

Practice loop

  1. Read Deep study (if present) or core concepts once.
  2. Recite the formula chain without looking.
  3. Open one easy pattern on the interactive atlas for week 5.
  4. Attempt without solutions; mark studied after an honest try.
  5. Say one trap aloud before closing the tab.
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.