Modules/Python/ Repeating Work

For Loops and range()

Do the same thing to every item, without writing it out every time. A for loop walks a collection; range() manufactures one to walk.

Walk the items you already have

A for loop takes each item in turn and binds it to the loop variable.


                        

Count with range, accumulate a total

range(n) produces 0 up to but not including n. Keeping a running total outside the loop is the workhorse pattern.


                        

What a for loop walks

The loop variable is yours to name. for colour in colours: reads better than for c in x: and costs nothing.
range(5) is 0,1,2,3,4 - five numbers, stopping before 5. That matches how indexing works, so the two fit together.
range(2, 10, 3) takes start, stop and step: 2, 5, 8.
A total you build up must be created before the loop. Create it inside and it resets on every pass, which is a bug that produces quietly wrong numbers rather than an error.

For Loops and range(): A Practical Guide

Say it once, run it many times.

Quick Context

A for loop repeats a block once per item in something iterable - a list, a string, a range. The loop variable is reassigned each time round, so the same code sees a different value on every pass. You do not manage a counter yourself unless you want one.

The accumulator pattern

Most useful loops build something up: a total, a count, a new list. The variable holding it has to exist before the loop starts, because the loop body adds to what is already there. Creating it inside the loop is the classic mistake - the code runs, and the answer is silently wrong.

Interactive Exploration Guide

  1. Run the first editor. Three colours, three lines. The second loop shows a string is iterable too - it walks characters.
  2. Run the second editor. Check that range(5) stops at 4, and follow the running total as it climbs 10, 30, 60.
  3. Move the accumulator inside. Put total = 0 inside the loop, indented with the body, and run. The final total becomes 30 - only the last item - with no error to warn you.
  4. Try a step. Change range(5) to range(0, 10, 2) and predict the output before running it.

Key Takeaway

A for loop walks the items of anything iterable and rebinds the loop variable each pass. range(n) counts 0 to n-1, which lines up with zero-based indexing. When accumulating a result, create the accumulator before the loop - putting it inside is a bug that returns a wrong answer rather than an error.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does range(5) produce?

  2. You want a running total. Where does `total = 0` belong?

  3. Can you loop over a string with a for loop?

Cheat sheet

For Loops and range()

A for loop repeats a block once per item in something iterable - a list, a string, a range. The loop variable is reassigned each time round, so the same code sees a different value on every pass. You do not manage a counter yourself unless you want one.

PYTHON · vizlearn.in/python/for_loops_and_range.html