successive modification pattern

A loop pattern where a variable is updated a little on every pass, each new value built from the previous one, such as number = number + 1.

In more detail

The variable must be given a starting value before the loop, and the update line sits inside the body. Numbering the entries of a list is the course's first example.

Example

n = 1
for item in items:
    print(n, item)
    n = n + 1

Related terms

Taught in

References