Unit 3 Β· Lesson 3

Loop Patterns

Most loops follow a handful of common patterns. Learn them once and you'll recognize them everywhere β€” they're the building blocks of real programs.

Master the accumulator pattern
Use counter and max/min patterns
Build strings inside loops
Combine loops with conditionals

Four Essential Patterns

Almost every loop you write will use one (or more) of these patterns:

πŸ“Š Accumulator

Build up a running total

total = 0
for x in data:
  total += x

πŸ”’ Counter

Count how many times something happens

count = 0
for x in data:
  if x > 10:
    count += 1

πŸ† Max / Min

Track the biggest or smallest value

biggest = data[0]
for x in data:
  if x > biggest:
    biggest = x

πŸ“ String Builder

Construct a string piece by piece

result = ""
for i in range(5):
  result += "*"

The += Shorthand Family

You've seen += for addition. The whole family works the same way:

x += 5 # same as x = x + 5 x -= 3 # same as x = x - 3 x *= 2 # same as x = x * 2 x //= 10 # same as x = x // 10 s += "!" # string concatenation works too!
Pattern recognition tip: When you see a problem, ask yourself: "Am I summing? Counting? Finding a max? Building a string?" The answer tells you which pattern to reach for.
Practice Time

Challenges

πŸ“Š

Challenge 1: Average Calculator

Guided

Calculate the average of a series of numbers using the accumulator pattern.

Instructions: Given a list of scores (we'll use a simple approach β€” the scores are in a range), calculate the sum of all numbers from 1 to n and then compute the average (as a float). Store the running sum in total.
Hint 1: for i in range(1, n + 1): then total += i and count += 1
Hint 2: For n=10: total=55, count=10, average=5.5
πŸ”’

Challenge 2: Digit Counter

Guided

Count how many digits a number has using the counter pattern.

Instructions: Given number, count its digits by repeatedly dividing by 10. Store the count in digits. Use a while loop: divide temp by 10 until it reaches 0, incrementing digits each time.
Hint 1: while temp > 0: then digits += 1 and temp //= 10
Hint 2: 34567 β†’ 3456 β†’ 345 β†’ 34 β†’ 3 β†’ 0 (5 steps = 5 digits)
⭐

Challenge 3: Star Triangle

Solo

Print a left-aligned right triangle made of * characters using the string builder pattern.

Instructions: Given rows, print a triangle where row 1 has 1 star, row 2 has 2 stars, etc.

Example (rows=4):
*
**
***
****
Hint 1: for i in range(1, rows + 1):
Hint 2: Each line: print("*" * i)
πŸ”

Challenge 4: Prime Checker

Stretch

Determine if a number is prime by checking for divisors.

Instructions: Set is_prime to True or False. A number is prime if it's greater than 1 and not divisible by any integer from 2 to num-1. Use a for loop to check for divisors. If you find one, set is_prime to False.

Optimization: you only need to check up to the square root, but checking up to num-1 is fine too!
Hint 1: First handle edge case: if num < 2: is_prime = False
Hint 2: for i in range(2, num): β€” if num % i == 0, it's not prime.
Hint 3: Inside the loop: if num % i == 0: is_prime = False. You can add break to stop early.