Looping over a known sequence: range in its 1-, 2- and 3-argument forms, walking through strings and lists, and choosing for vs while.
01
Learning objectives
Write for loops over range() in all three forms: range(stop), range(start, stop), range(start, stop, step).
State and apply the rule that the stop value is excluded.
Iterate directly over a string's characters and a list's items.
Choose sensibly between for (known collection) and while (unknown count).
Predict the loop variable's value after the loop ends.
02
Lesson notes
The for loop is Python's tool for visiting every item of a known sequence, one at a time. Its shape reads like English: for item in sequence: — 'for each item in the sequence, do the indented body'. The loop variable (item) is automatically assigned each value in turn; you never update it by hand. This is the biggest practical difference from while: while counts passes manually, for hands you the values directly.
Most often the sequence is produced by range(), and its three forms must be memorised as a set. range(5) is 0, 1, 2, 3, 4 — five numbers starting at 0, and NOT including 5. range(2, 8) is 2 through 7 — stop excluded again. range(0, 10, 2) steps by 2: 0, 2, 4, 6, 8. The stop-excluded rule is the same slice rule you learned with strings, and it means len(sequence) is always exactly stop − start when step is 1 — the loop count is readable straight off the arguments.
Because strings ARE sequences, for walks them character by character with no indexes at all: for ch in "PYTHON": print(ch) prints the six letters downwards. The same pattern runs through lists (next module) — for mark in marks_list: — and this 'iterate directly' style is considered the most Pythonic. When you genuinely need position numbers (like a numbered guest list), combine them: for i in range(len(word)): — a pairing so common it appears on virtually every exam paper.
for and while overlap — any for can be rewritten as a while — but the idiomatic split is simple. Use for when the collection is known in advance: every character, every item, a fixed number of repetitions. Use while when the repetition count is unknown until runtime: user input, sensor readings, a search that ends early. Writing for i in range(1000000) hoping the user types something in the middle is a beginner smell; a sentinel while is the honest tool for that job.
Two final details close the lesson. First, the loop variable survives the loop with the LAST value it received: after for i in range(4), i is 3 — the last item, not the stop value (a delightful contrast with while, where the counter ends on the first FAILING value). Second, range() is lazy: it produces numbers as asked rather than building a list in memory, which is why range(10_000_000) costs no memory. The next lesson builds on both facts to construct the accumulator patterns that dominate the exam's loop questions.
A for loop repeats over a sequence
03
Worked code examples
The three forms of range()
range(5) starts at 0 and excludes 5; range(2, 8) excludes 8; range(0, 10, 2) jumps by 2. In every form the stop is never included.
for n in range(5):
print(n, end=" ")
print()
for n in range(2, 8):
print(n, end=" ")
print()
for n in range(0, 10, 2):
print(n, end=" ")
Output
0 1 2 3 4
2 3 4 5 6 7
0 2 4 6 8
for over a string — with and without indexes
The first loop hands you characters directly — the Pythonic default. The second pairs each position with its character via range(len(word)), the standard pattern when the index itself matters.
word = "QUIZ"
for ch in word:
print(ch, end="!")
print()
for i in range(len(word)):
print(i, word[i])
Output
Q!U!I!Z!
0 Q
1 U
2 I
3 Z
Counting down and the surviving loop variable
A negative step counts down — and even here the stop (0) is excluded. After the loop, t keeps the LAST value it was given (1), unlike a while counter that ends on the first failing value.
for t in range(3, 0, -1):
print(t, "...")
print("Go!")
print("t is now", t)
Output
3 ...
2 ...
1 ...
Go!
t is now 1
04
Key terminology
loop variable
The name after for that receives each sequence value in turn; it survives the loop with the last value.
range(start, stop, step)
Builds the integer sequence for for loops; stop is always excluded.
iterate
To visit each item of a sequence once — what a for loop does.
sequence
Anything whose items come in order: strings, lists, ranges.
range(len(s))
The pairing pattern that yields every index of s, used when positions matter.
05
Common misconceptions
Common mistake
range(1, 5) produces 1, 2, 3, 4, 5.
Correct understanding
The stop is excluded: 1, 2, 3, 4 — four numbers. Range behaves exactly like slices.
Common mistake
range() must be given a list or string to work.
Correct understanding
range() generates its own integer sequence — that is its purpose; no existing collection is needed.
Common mistake
for loops can only count numbers; strings need while loops.
Correct understanding
Strings and lists are sequences and for walks them directly — for ch in word is the idiomatic form.
06
Check your understanding
1. How many numbers does range(2, 8) produce, and what is the last one?
2. What does this print?
for c in "ABC":
print(c, end="-")
3. After for i in range(4): print(i) finishes, what is i?