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 + 1Related terms
- accumulation pattern — A loop pattern that builds a result step by step, starting from an initial value and combining each item into it, such as adding up a list.
- initialisation — Giving a variable its first value before it is used, especially before a loop that updates it.
- for loop — A loop that runs its body once for each item in a list or other collection, setting a loop variable to the current item.
Taught in
- Repeat Loops And Successive Modification — Hands-on Python: Complete Video Course & Book
- Accumulation Loops — Hands-on Python: Complete Video Course & Book
- Mad Libs Revisited: The Whole Program — Hands-on Python: Complete Video Course & Book