Concepts / for Loops

for Loops

A for loop repeats a block of code for each item in a sequence, assigning each item to a loop variable one at a time.

  • Programming
Interactive lab

Try it: Loop Tracer

How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.

How it works

  1. Initialise the accumulator before the loop.
  2. Each iteration, the loop variable takes the next value from the list.
  3. An if inside the loop decides whether to update the accumulator.
  4. After the last item the loop ends and the accumulator holds the answer.

Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.

Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.

Educational simulation

Loading the simulation…

A Sequence of Small Steps

A for loop is useful when the same block of code must be repeated for each item in a sequence. Instead of treating the repetition as one large action, the loop visits one item, assigns that item to a loop variable, runs the loop body, and then moves to the next item. This continues until the sequence has no items left.

In the real world

Suppose a task requires processing every item in a known sequence. A for loop matches that task because its basic operation is to take one item at a time and repeat the same block for each item.

The Loop Variable Moves

The loop variable is the name that receives the current item. On the first iteration, it receives the first item in the sequence. After the loop body finishes, it receives the next item, and the body runs again. The loop variable therefore represents the item currently being processed, not the entire sequence at once.

Sequencered, blue, greenredcurrent itemLoop bodyruns once per itembluecurrent itemgreencurrent itemfirst itemrunnext itemrunnext itemrun
What happens to the loop variable and loop body as the loop visits each item in the sequence?
python
Output
red
blue
green

Reading the for...in Pattern

Definition

A for loop repeats a block of code for each item in a sequence, assigning each item to a loop variable one at a time.

Code walkthrough
for item in sequence:
    process(item)
  1. 1
    The loop obtains the next item from the sequence.
  2. 2
    That item is assigned to item.
  3. 3
    The indented loop body runs using the current value.
  4. 4
    The loop returns to obtain the next item and repeats until the sequence is exhausted.
  • for begins the iteration statement.
  • item is the loop variable that receives the current sequence item.
  • in connects the loop variable to the sequence being visited.
  • sequence supplies the items one at a time.
  • process(item) is the loop body and runs for each item.
Sequenceitems availableNext itemassign to loop variableLoop bodyruns onceMore itemsyes or noLoop finishedsequence exhaustedvisitruncheckyesno
How does execution move from obtaining the next item to running the loop body and back until the sequence is exhausted?

Generating Number Sequences

The range() function generates a sequence of numbers. With range(start, stop), the sequence begins at start and continues up to, but does not include, stop. If no step is supplied, the sequence increases by 1. A third parameter controls the step size, so a range can count by larger increments or count backwards.

2start4next by 26next by 28stop, excluded+2+2stop before
Which numbers does range(start, stop, step) generate, where does it begin and end, and how does the step change the sequence?
python
Output
2
4
6

Tracing Each Iteration

What do you think happens?

What output is produced, and what value does number have during each iteration?

  • 1, 2, 3

  • 1, 2

  • 0, 1, 2, 3

Reveal answer
Answer:

The output is 1, 2, 3, with each value printed on its own line.

The loop variable receives 1 first, then 2, then 3. The stop value 4 is excluded.

Code walkthrough
for number in range(1, 4):
    print(number)
  1. 1
    First iteration: number is 1, so the loop body prints 1.
  2. 2
    Second iteration: number is 2, so the loop body prints 2.
  3. 3
    Third iteration: number is 3, so the loop body prints 3.
  4. 4
    The sequence has no further values before 4, so the loop ends.
  • range(1, 4) generates the values 1, 2, and 3.
  • number receives 1 during the first iteration, 2 during the second, and 3 during the third.
  • print(number) runs once for each value.
number = 1print 1number = 2print 2number = 3print 3Finishedno next itemnextnextsequence exhausted
What is the loop variable, what code runs, and what output is produced during each iteration?

Mistakes Beginners Make

  • Common mistake

    Assuming that the stop value is included.

    What it looks like
    Treating range(1, 4) as the values 1, 2, 3, and 4.
    Why it fails

    The range sequence goes up to but does not include its stop value.

    Do this instead

    Trace the generated values as 1, 2, and 3.

  • Common mistake

    Treating the loop variable as the whole sequence.

    What it looks like

    Thinking number represents every value in range(1, 4) during every iteration.

    Why it fails

    The loop variable receives one current item at a time.

    Do this instead

    Track its value separately for each iteration.

  • Common mistake

    Forgetting that the loop body repeats.

    What it looks like

    Expecting print(number) to run only once in a loop over several numbers.

    Why it fails

    The loop body runs for each item in the sequence.

    Do this instead

    Count the sequence items and trace one body execution for each.

  • Common mistake

    Using the wrong step direction.

    What it looks like

    Expecting a positive step to count backwards.

    Why it fails

    The step controls how the generated sequence changes, including whether it counts by different increments or backwards.

    Do this instead

    Choose a step that matches the intended direction and increment.

Choosing the Structure

Choose a for loop when the task requires the same processing for each item in a known sequence. The sequence may provide its items directly, or range() may generate a sequence of numbers with the required start, stop, and step. Before writing the loop, identify the sequence, the loop variable, and the action that should happen once per item.

  1. 1
    Identify the sequence that should be visited.
  2. 2
    Choose a loop variable name for the current item.
  3. 3
    Write the loop body as the action that should occur once per item.
  4. 4
    Trace the first item, the next item, and the final item to check the expected result.
EASY

Trace this loop before checking the result. List the value of step during each iteration and the output produced:

for step in range(3, 10, 3):
    print(step)
Hints
  • Begin at the start value, 3.
  • Add the step value, 3, after each iteration.
  • Stop before reaching the stop value, 10.

Key Takeaways

  1. 1
    A for loop repeats its body once for each item in a sequence.
  2. 2
    The loop variable receives one current item at a time.
  3. 3
    range(start, stop) begins at start, excludes stop, and uses a step of 1 by default.
  4. 4
    The third range() parameter controls the step, allowing different increments or backwards counting.
  5. 5
    Tracing the sequence of loop-variable values makes the loop's output predictable.

At a glance

  • A for loop processes a sequence one item at a time.
  • Each iteration assigns the current item to the loop variable and runs the loop body.
  • range() creates number sequences using start, stop, and optional step values.
  • The stop value is excluded from the generated sequence.
  • A for loop is appropriate when the same action must be performed for every item in a known sequence.

Related concepts

More concepts starting with F