Max, Min, and Counter Patterns in Python Loops

Three loop shapes with names — recognise them on sight instead of reinventing them each time.

  • Lesson 1 of 4
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Tracking patterns: max, min, and count

  1. The dragon: You've already written loops that carry a value forward — a running total. There are two more shapes just as common, and they all have names.

  2. The dragon: Max-tracking: start with a 'best so far' guess, then update it only when something BIGGER shows up.

    example.pyPython 3
    scores = [40, 85, 12, 90, 33]highest = scores[0]for s in scores:    if s > highest:        highest = sprint(highest)
    Output90
  3. The dragon: Notice the shape: ONE box (highest), a loop, an if that only updates it SOMETIMES. That shape is the whole pattern — the rest is just what you're comparing.

  4. The dragon: Min-tracking is the exact same shape with the comparison flipped — smaller instead of bigger.

    example.pyPython 3
    prices = [80, 20, 55, 15, 90]lowest = prices[0]for p in prices:    if p < lowest:        lowest = pprint(lowest)
    Output15
  5. The dragon: Counting is a third shape: start a counter at 0, and add 1 each time something passes a test — never add the VALUE, just count how many qualify.

    example.pyPython 3
    hoard = [10, 200, 5, 300, 45, 8]count = 0for v in hoard:    if v > 100:        count = count + 1print(count)
    Output2
  6. The dragon: All three patterns walk a list ONCE, carrying one value forward. Once you recognise which one a problem needs, you don't have to invent the shape from scratch — you already know it.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    You need to find the SMALLEST value in a list of temperatures. Which pattern fits?

    • Min-tracking — start with a 'lowest so far' guess, update when something smaller shows upCorrect
    • The counter pattern — count how many temperatures there are
    • Max-tracking, since it's basically the same idea

    Why: Min-tracking. It's the pattern built specifically for 'find the smallest' — start with a guess, and only update when something smaller actually shows up. Max-tracking asks the opposite question, and counting asks a completely different one.

  2. Question 2 of 2

    hoard = [10, 200, 5, 300, 45, 8]. Using the counter pattern to count values over 100, what's the final count?

    question.pyPython 3
    count = 0for v in hoard:    if v > 100:        count = count + 1
    • 2 — only 200 and 300 are over 100Correct
    • 500 — the counter pattern adds up the qualifying VALUES, not how many there are

    Why: 2. Only 200 and 300 pass the v > 100 test, and the counter pattern adds exactly 1 each time that happens — never the value. count = count + 1 is a different move from total = total + v, even though both carry a running value forward through the loop.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    scores = [40, 85, 12, 90, 33]. Find the HIGHEST score by tracking it as you go: start with the first score as your best guess, then compare every OTHER score against it, updating whenever you find something bigger. Print the final highest score — 90.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.