Looping over lists
Python · Unit 5: Lists
- Full plan
- Python
- 11 exercises
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. Predict the output
What does this code print?
2. Multiple choice
In
for x in [3, 9, 5]:, what isxon each pass?3. Complete the code
Complete it to count how many scores are passing (60 or more).
4. Predict the output
What does this code print?
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. Predict the output
What does this code print?
7. Complete the code
Complete it so Liam can get into the party.
8. Complete the code
Complete it to print the place and the name of each person.
9. Find the bug
This program should print just once how many perfect scores there are. Which line has the error?
10. Put the lines in order
Put in order the function that returns a list with only the even numbers.
11. Predict the output
These are the sales for each day. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.