Skip to the content

Looping over lists

Python · Unit 5: Lists

for can walk through a list too: on each pass, the variable takes the next item. You need no range and no indexes.

for fruit in ["mango", "pear"]:
    print("Today:", fruit)

Prints

Today: mango
Today: pear

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Multiple choice

    In for x in [3, 9, 5]:, what is x on each pass?

  3. 3. Complete the code

    Complete it to count how many scores are passing (60 or more).

  4. 4. Predict the output

    What does this code print?

  5. 5. Find the case that fails

    largest(nums) should return the biggest number in the list. It never gets an empty list. Which call makes it fail?

  6. 6. Predict the output

    What does this code print?

  7. 7. Complete the code

    Complete it so Liam can get into the party.

  8. 8. Complete the code

    Complete it to print the place and the name of each person.

  9. 9. Find the bug

    This program should print just once how many perfect scores there are. Which line has the error?

  10. 10. Put the lines in order

    Put in order the function that returns a list with only the even numbers.

  11. 11. Predict the output

    These are the sales for each day. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →