Loops repeat work, but most beginner problems use only a few loop shapes. Learning those shapes makes new questions feel familiar.

Count, collect, or search

A counting loop adds one when an item matches a rule. A collecting loop builds a new result. A searching loop can stop as soon as it finds what it needs.

  • Count items that pass a test.
  • Collect changed items into a new list.
  • Search until the answer appears.
  • Compare each item with the best one seen so far.

Give the loop one clear job

A loop is easier to trust when its state has a simple meaning. In the example below, even_count always means the number of even values we have seen.

Count values that matchPython
def count_even(numbers):
    even_count = 0

    for number in numbers:
        if number % 2 == 0:
            even_count += 1

    return even_count

print(count_even([2, 7, 8, 10]))  # 3

When a problem looks complicated, ask which small loop shape sits inside it. You can often solve one part at a time and then combine the pieces.

Your tiny challenge

Write a loop that collects every word longer than four letters. What does your result list mean after each step?