Lazo Lab Sign up

Python Fundamentals / Loops

Lesson 19 of 38

Loop patterns: totals and counts

Two classic patterns: keep a running total by adding inside the loop, and count matches with a counter that starts at 0.

Key points

  • Start totals at 0
  • Add or count inside the loop
  • Print after the loop ends
total = 0
for n in [4, 8, 15]:
    total += n
print(total)  # 27
Watch a video on thisOpens YouTube search results for “Loop patterns: totals and counts” in a new tab

Quiz · +10 XP

What does total hold after adding 2, 3 and 5 starting from 0?

Log in to save progress and earn XP.