Skip to content
dreamcode
dreamcode
Map
Loop patterns
Lesson 19 of 77
+15 XP on finish
LOOPSChapter 3 · Loops and iteration

Four loop patterns

Most loops follow one of a few patterns. An accumulator starts at 0 and adds as it goes. A counter adds 1 when something matches. A tracker remembers the best value seen so far. A collector starts with an empty list and appends. Learn these four shapes and most loop problems become fill in the blanks.

Worked example

How it reads

  • Accumulator: total += h adds every value
  • Counter and collector: tall += 1 and big.append(h) only when the condition holds
  • Tracker: start with the first item, replace it when something beats it
Cloud tip: Start a tracker with the first item, not with 0. If every value is negative, starting at 0 gives the wrong answer.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Use a tracker loop to find the lowest temperature and print it (6). Try it without min().

Press Run to check your work.