Stop 03 · Control FlowTopics · 5/8

For Loops & range()

Part of the Python Roadmap

In short

Sending a reminder to 200 students, adding up a month of expenses, checking every row of a file: loops are how one line of logic gets applied to many things without copy-pasting.

A for loop walks through any iterable, such as a list, string or range. range(start, stop, step) produces numbers on demand and stops before the stop value.

The jaadu walks down the aisle of a city bus with a ticket punch, stopping at each seated passenger in turn.

Think of it like this

The bus conductor's round

A conductor walks down the bus from the first seat to the last, stopping at each passenger to check the ticket. Same action, every passenger, in order, and when the last seat is done, the round ends. A for loop is that conductor walking through your list.

Watch it happen

for n in range(2, 11, 2):

2
0
4
1
6
2
8
3
10
4

1/5range(2, 11, 2) starts at 2, goes up in steps of 2 and stops before 11.

The big ideas

What you need to know

for item in collection

A for loop takes each item from any iterable, like a list, a string, a range or a file, and runs the indented block once per item.

range(stop)

range(5) gives 0, 1, 2, 3, 4. Five numbers, starting at 0, never including the stop value.

range(start, stop, step)

range(1, 11) is 1 to 10, range(0, 50, 10) is 0, 10, 20, 30, 40, and range(10, 0, -1) counts down.

The accumulator pattern

Start total = 0 before the loop, add to it inside, use it after. It is the most common loop pattern there is.

See it in code

Walk through the code

python
1cities = ["Delhi", "Mumbai", "Kolkata"]2for city in cities:3    print(f"Booking trains to {city}")4 5for letter in "chai":6    print(letter.upper(), end=" ")   # C H A I7print()8 9for floor in range(10, 0, -3):10    print(floor, end=" ")            # 10 7 4 111print()12 13total = 014for n in range(1, 101):15    total += n16print(total)                         # 5050

1/4city takes each value in turn. The indented print runs three times.

Loop over a list, a string and a range, then the accumulator pattern adds 1 to 100.

Save yourself the pain

Common mistakes

Oops

Expecting range(1, 10) to include 10.

Do this instead

The stop value is always excluded. Use range(1, 11) for 1 to 10.

Oops

Putting print(total) inside the loop when you only want the final answer.

Do this instead

Dedent it so it runs once after the loop. Indentation decides what repeats.

Oops

Writing for i in range(len(names)): print(names[i]).

Do this instead

Loop over the items directly: for name in names: print(name). Use enumerate() when you also need the position.

Your mission

Loop drills

~20 min

Quick check

Which numbers does range(3, 12, 4) produce?

Go deeper

  1. 1Print the 7 times table using range(1, 11).
  2. 2Loop over a string and count its vowels.
  3. 3Use range(10, 0, -1) to count down.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge