Python: warming up...

Loops Trainer

Built-in AI tutor. Stuck on a cell? Ask the helper on this page. It coaches, it does not hand you the answer. Try a run first.

In Excel you wrote one formula and dragged it down every row. Python's for loop is the same move: write the step once, and it runs once per item in a list. This is a notebook — type a line in a cell and press Shift+Enter (or click Run) to execute it. The output appears right under the cell, like Jupyter or Colab. The Python is real, running sandboxed in your browser.

The bridge: drag-down is a for loop

Same three invoice amounts. On the left you drag =B2*1 down column C and Excel evaluates it once per row. On the right the loop body runs once per item. One step, repeated down the column.

Excel — drag the formula down

B (amount)C (=B*1)
21200.001200.00
3340.50340.50
489.0089.00

Python — for loop

for amount in amounts:
    # this line runs once per amount
    print(amount)

Each Excel cell that fills in = one trip through the loop body.

Why it matters. The dragged formula and the loop body are the same idea: a step you define once and apply to every row. When the data has 3 rows you could drag by hand. When it has 30,000, the loop is the only thing that does not get tired or skip a row.

The notebook

You must run these cells in order, top to bottom. Each one uses names defined by the cells above it. If you skip a cell, the ones below it will fail with a NameError.

Predict the output first, then run and compare. Edit any cell and re-run it — that is how you learn what each line does.

What you just practiced

The three loop moves. Label each row — the loop body prints or formats one line per item. Running total — start an accumulator at 0 and add to it with total += amount inside the loop, the way a running-total column carries the sum down the page. Iterate rows — loop over a list of records (each a dict) and pull fields out by name, like reading across one spreadsheet row at a time. Every one of these is "do this once per row," which is exactly what dragging a formula down does.