1.2.18 · D1Introduction to Programming (Python)

Foundations — for loop — iterating over sequences

1,953 words9 min readBack to topic

Before you can read the parent note fluently, you need a small pile of building blocks. Below, each one is given in plain words → a picture → why the topic needs it, in an order where every block rests on the one before it.


1. A value, and a name (variable)

Picture a value as a box sitting on a shelf, and a variable as a luggage tag tied to that box with a string. The tag isn't the box — it just points at it.

Figure — for loop — iterating over sequences

Why the topic needs it: the loop line for item in seq: creates a name item and re-ties that tag to a different value on every pass. If you don't picture a name as a re-tie-able tag, the loop variable feels like magic. It isn't.


2. A sequence (an ordered collection)

Picture a row of lockers, left to right. Locker 0 holds the first value, locker 1 the next, and so on. "Order" just means the lockers have fixed positions you could point at.

Figure — for loop — iterating over sequences

Why the topic needs it: the whole reason for exists is to visit each locker in turn. See Lists and indexing for the list flavour and Strings as sequences for why "cat" is a row of the single characters c, a, t.


3. Index — the position number

On the locker row above, the index is the number painted above each locker. The syntax seq[1] means "open the locker at position 1 and hand me what's inside" — here, 20.

Why the topic needs it: the parent note's mistakes ("modifying a list while looping", "the iterator tracks a position index") only make sense once you see the cursor as an index that moves.


4. Length, and the half-open idea [start, stop)

Why the topic needs it: range(2, 8) uses this exact rule, giving 2, 3, 4, 5, 6, 7. And range(len(x)) gives valid indices 0 .. len−1 precisely because the stop is excluded. Get this backwards and every counting loop is off by one. See range function.


5. Iterable — "loopable"

Picture the locker row again, but now with a rule pinned to it: "there is a defined way to visit us in order." That pinned rule is what makes it iterable — not the lockers themselves, but the promise that they can be visited.

Why the topic needs it: for item in X requires X to be iterable. This is the single precondition of the whole construct.


6. Iterator and cursor — "where am I now?"

Picture a finger sliding along the locker row. iter places the finger before locker 0. Each next says "give me the locker the finger is about to pass, then slide the finger one to the right."

Figure — for loop — iterating over sequences

Why the topic needs it: the parent proves for is "just" iter once + next repeatedly. And the dangerous mistake — deleting from a list mid-loop — is entirely a story about this finger: remove an item and everything shifts left under the finger, so it skips one. See Iterators and generators.


7. StopIteration — the "empty!" signal

Picture the sliding finger reaching past the last locker: there's no locker there, so instead of a value it raises a little red flag. The for loop watches for that flag and quietly ends. No counting, no off-by-one — the data itself tells the loop when to stop.

Why the topic needs it: this is the clean stopping mechanism that makes for safer than manual index counting. It's why the parent's desugared while True: loop can loop forever yet still terminate correctly.


8. Tuple and unpacking — bundling values

Picture a stapled pair of cards (index, item). Unpacking un-staples them and lays each on its own named tag.

Why the topic needs it: enumerate hands you (index, item) bundles and zip hands you (a, b) bundles. The loop header for i, fruit in enumerate(...) unpacks each bundle automatically — no bundle, no clean counter. See enumerate and zip.


How these blocks feed the topic

value + variable = name tag

sequence = ordered row

index = position number from 0

length + half-open start,stop

iterable = loopable

iterator + cursor

iter() once, next() repeatedly

StopIteration = empty signal

tuple + unpacking

for loop

Read it bottom-up: the for loop at the base rests on all of these. Everything the parent note says is a combination of "name tag", "ordered row", "position number", "the cursor that raises StopIteration", and "unpack the bundle".


A tiny end-to-end trace


Equipment checklist

Test yourself — cover the right side and answer aloud.

What is the difference between a value and a variable?
A value is the data; a variable is a re-attachable name tag pointing at it.
What makes something a "sequence"?
Its items have a fixed left-to-right order (a first, second, … last).
What index does the first item of a list have, and why?
Index 0 — it takes 0 steps past the start to reach it.
What does the half-open notation mean?
Start included, stop excluded; it contains stop − start numbers.
What does range(2, 5) produce?
2, 3, 4 (stop 5 excluded).
What is an iterable versus an iterator?
An iterable is anything you can loop over; an iterator is the helper that remembers where you are (holds the cursor).
What do iter() and next() each do?
iter makes a fresh iterator with the cursor at the start; next hands the next item and slides the cursor forward.
What signals that a loop has run out of items?
StopIteration, raised by next() when the cursor is past the last item.
What is tuple unpacking, and where does the loop use it?
Splitting a bundle like (0, "a") into separate names; used in for i, v in enumerate(...) and zip.
Why does deleting an item mid-loop skip an element?
The cursor is a position index; removing shifts later items left under it, so it steps over the next one.

Connections

  • 1.2.18 for loop — iterating over sequences (Hinglish) — parent topic in Hinglish.
  • range function — the half-open counting iterable built on the index idea.
  • Lists and indexing — the ordered row + position numbers made concrete.
  • Strings as sequences — why a string is a row of single characters.
  • Dictionaries — keys, values, items — an iterable whose "items" are key–value bundles.
  • enumerate and zip — where tuple bundling and unpacking pay off.
  • Iterators and generators — the deeper story of the cursor and StopIteration.
  • while loop — condition-based repetition — the raw loop for desugars into.
  • List comprehensions — a compact loop that reuses every block above.