×

python · level 1 · unit 6 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A for loop can visit every item in a list.

Think of a checkout. The cashier picks up each thing in your basket, one at a time, and beeps it. for coin in coins: does the same: each turn, coin holds the next item in the list.

No range needed. The loop runs once for every item, then stops by itself after the last one.

To add things up, keep a . Start a variable at 0 before the loop. Each turn, add the item to it. After the loop, it holds the full total.

coins = [2, 5, 1, 2]
total = 0
for coin in coins:
    total = total + coin
print("Piggy bank:", total)

Name the loop variable after one item: coin for coins, pet for pets.