SypherTec
…
PART 2 · 05 OF 084 MIN READUPDATED 20 NOV 2026

Accumulation: Counting and Adding Up in a Loop

Your report lists every expense, but the loop only prints them. This lesson has the loop keep count as it goes, and report how many of your expenses were over ₦3,000.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Now run this, and watch the numbers climb:

A RUNNING TOTAL · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

running started at 0 before the loop. On every pass, it became its old value plus the current amount: 1500, then 4000, then 7500, and so on. After the last pass, it matched sum exactly.

That is all sum does. The pattern underneath is worth knowing, because it answers questions no built-in function can. A variable that starts before a loop and changes on every pass is called an accumulator.

Here is a question sum cannot answer: how many expenses were over 3,000?

COUNTING THE ITEMS THAT MATCH · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

large started at 0. On each pass, the if asked whether the current amount was over 3,000. Only when it was did large go up by one. Two amounts passed the test, so large ended at 2.

Look at the indentation. The if is inside the loop, so it runs on every pass. The line that adds one is inside the if, so it runs only on the passes where the condition is True. The final print is outside both, so it runs once, after the loop.

The syntax

large = 0
for amount in expenses:
    if amount > 3000:
        large = large + 1
print(large)

Three parts, always in this order:

  1. Start the accumulator before the loop: 0 for a count or a total.
  2. Update it inside the loop: large = large + 1 to count, running = running + amount to add up.
  3. Read it after the loop, once every pass has finished.

large = large + 1 reads as "work out large + 1, then store the result back in large". Python has a short form that does the same thing: large += 1. Both are correct. This track writes the long form so each step stays visible.

A minimal working example

main.py
expenses = [1500, 2500, 3500, 800, 4200]
 
large = 0
for amount in expenses:
    if amount > 3000:
        large = large + 1
 
print(f"Expenses over ₦3,000: {large}")

Output: Expenses over ₦3,000: 2. Change the 3000 in the condition, and the same loop counts against a different limit.

Common mistakes

You start the accumulator inside the loop. Run this:

THE START VALUE INSIDE THE LOOP · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

It printed 1, not 2. large = 0 ran at the start of every pass, so each pass wiped out the count before it. Only the last pass survived, and 4200 is over 3,000, so the answer was 1. Start the accumulator once, before the loop.

You never start it at all. Run this:

NO START VALUE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
NameError: name 'large' is not defined. Did you mean: 'range'?

On the first matching pass, large = large + 1 needs the current value of large, and there is none yet. Python's suggestion is a guess based on spelling, and here it is wrong. Add large = 0 above the loop.

You work out the new value but never store it. large + 1 on a line of its own gives no error, and changes nothing. The count stays at 0. Write large = large + 1, or large += 1.

Try it yourself

You will count your large expenses inside the loop that lists them.

  1. Run the starting code below. Confirm your report lists your expenses.

  2. Directly above your for line, add large_spends = 0, at the same indentation as the for.

  3. Inside the loop, under the print, add an if that counts amounts over 3,000:

        if amount > 3000:
            large_spends = large_spends + 1

    The if is indented four spaces, to sit inside the loop. The line under it is indented eight.

  4. Under your Smallest spend print, add print(f"Expenses over ₦3,000: {large_spends}"). Run it. Confirm the new line matches the number of amounts over 3,000 in your list. With the amounts given, it is 2.

  5. Break it on purpose: move large_spends = 0 inside the loop, as its first line, indented four spaces. Run it. Confirm the count drops: it now shows 1 if your last expense is over 3,000, and 0 if not.

  6. Fix it: move large_spends = 0 back above the for line, with no indentation. Run it. Confirm your count is correct again.

TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your loop now counts as well as prints. Every loop so far has run once per item in a list. The next lesson meets a loop that runs until something changes, however many passes that takes.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 2 · 05 OF 08❮❯