SypherTec
…
PART 2 · 04 OF 083 MIN READUPDATED 20 NOV 2026

for Loops: Once for Every Item

Your report gives totals, but not the expenses behind them. This lesson lists every expense on its own line, without one print per item, so the list can grow and the report keeps up.

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:

ONE LINE, THREE TIMES · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Three amounts on three lines from a single print. That is a for loop. It runs the indented block under it once for every item in the list, in order. Before each pass, Python stores the next item under the name amount. So the block prints 1500, then 2500, then 3500. Done is not indented, so it runs once, after the loop has finished.

The name between for and in is the loop variable. You choose it. A good name says what one item is: one amount from a list of expenses.

A loop's block can hold several lines, and all of them run on every pass:

A BLOCK OF TWO LINES · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Two items, two passes, four lines. The block is everything indented under the for line, exactly as with if.

The syntax

for amount in expenses:
    print(f"  ₦{amount:,}")
  • for, a name of your choice, in, the list, and a colon.
  • The block is indented by four spaces.
  • During each pass, the loop variable holds the current item.
  • The loop ends when every item has had its turn. Python then carries on from the first line after the block.

A for loop is the reason a list beats separate variables. Add an expense to the list, and the loop prints it too. You never write another line.

A minimal working example

main.py
expenses = [1500, 2500, 3500, 800, 4200]
 
print("Expenses:")
for amount in expenses:
    print(f"  ₦{amount:,}")

Output: Expenses:, then the five amounts, each with a naira sign. The two spaces at the start of the string indent each amount in the report. They are part of the text, not the code's indentation.

Common mistakes

You print the list instead of the item. Run this:

THE LIST INSTEAD OF THE ITEM · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The whole list printed three times. The block runs once per item, and on each pass it printed expenses, the entire list. Inside the loop, use the loop variable, amount, to work with one item.

You loop over a number. Run this:

A LOOP OVER A NUMBER · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: 'int' object is not iterable

"Iterable" means something Python can go through item by item, like a list. total is one number, with no items. Loop over the list itself: for amount in expenses:.

You forget the colon or the indentation. These give the same errors as with if: SyntaxError: expected ':', and IndentationError: expected an indented block after 'for' statement. Every line that opens a block follows the same two rules.

Try it yourself

You will add a list of every expense to the top of your report.

  1. Run the starting code below. Confirm your report shows totals but no individual expenses.

  2. Find the print() that makes the blank line under your month. Under it, add these lines:

    print("Expenses:")
    for amount in expenses:
        print(f"  ₦{amount:,}")
    print()

    Run it. Confirm the report lists every expense on its own line, each starting with ₦, followed by a blank line and then Expenses recorded.

  3. Add a sixth expense: put , 1200 before the closing ] on your expenses line. Run it. Confirm the list in your report gained a line, without you writing another print.

  4. Remove the , 1200 you added. Run it. Confirm the list is back to its earlier length.

  5. Break it on purpose: delete the four spaces in front of the print inside your loop. Run it. Confirm the console shows an IndentationError that mentions the 'for' statement.

  6. Fix it: put the four spaces back. Run it. Confirm your expense list returns.

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

Your tracker lists every expense, however many there are. The loop only prints, though. The next lesson has it count and add up as it goes, so it can answer questions sum and max cannot.

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