AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Cases and lists

Week 8 · 1.5 Programming with data · 3 lessons
3

Loop over a list

Computing 9: Topic 1.5, pp. 89–118
GoalUse for loops to total, count and compare the items in a list, with an IF nested inside the loop.
New words
nesting · putting one construct inside another, such as an IF inside a for loopitem · one value stored in a listrunning total · a variable that adds each item to the amount stored so far
Explanation

A list and a loop work well together. One way is for i in range(len(rain)): the counter i is each index, and rain[i] is the item. Use this when you also need the position. The other way is for amount in rain:. Here the variable amount holds each item in turn, from the first to the last, and no index is needed. Use this when only the values matter. Inside the loop you can use the usual patterns. A running total is set to 0 before the loop. A counter adds 1 when an IF finds a matching item. A best-so-far variable starts with the first item and is replaced when a better one comes. The IF is nested inside the for, so its lines are indented twice.

Python
1rain = [0, 12, 5, 0, 20, 7]
2total = 0
3for amount in rain:
4    total = total + amount
5print("Total:", total)
Output
Total: 44
Worked examples
Python
1rain = [0, 12, 5, 0, 20, 7]
2for i in range(len(rain)):
3    print("Day", i + 1, rain[i])
Output
Day 1 0
Day 2 12
Day 3 5
Day 4 0
Day 5 20
Day 6 7
The index version, used when the day number is needed. i is 0 to 5, so the program prints i + 1 as the day number and rain[i] as the amount. This visits the same six items as the picture above.
Python
1rain = [0, 12, 5, 0, 20, 7]
2dry = 0
3biggest = rain[0]
4for amount in rain:
5    if amount == 0:
6        dry = dry + 1
7    if amount > biggest:
8        biggest = amount
9print("Dry days:", dry)
10print("Wettest:", biggest)
Output
Dry days: 2
Wettest: 20
Two IFs nested in one loop. The first counts the days with no rain. The second keeps the biggest amount, starting from the first item. After the loop it prints Dry days: 2 and Wettest: 20.
amountdrybiggest
(before)00
010
12112
5112
0212
20220
7220
The same program traced item by item. The first column is the item taken from the list. The 0 on the first and fourth rows adds to dry. The values 12 and 20 replace biggest, and the others change nothing.
Class activity

Row of cards (no computer). Write six numbers between 0 and 30 on cards and lay them in a row, face up. Make a trace table with the columns item, total and biggest. Move a finger along the row. At each card, update the total and compare with the biggest so far. Then turn all the cards face down, put one random card up at a time, and decide how you would find the smallest by comparing with the smallest so far. Finally, count how many cards are greater than 10.

Practice
1
What does this program print?
Python
1temps = [21, 24, 19, 23]
2total = 0
3for t in temps:
4    total = total + t
5print(total / len(temps))
2
Trace the program with a table for s and smallest. What does it print?
Python
1sizes = [8, 3, 9, 1, 6]
2smallest = sizes[0]
3for s in sizes:
4    if s < smallest:
5        smallest = s
6print(smallest)
3
What does this program print? Why does it use a.lower()?
Python
1answers = ["Yes", "no", "YES", "yes", "No"]
2count = 0
3for a in answers:
4    if a.lower() == "yes":
5        count = count + 1
6print(count)
4
Write a program that stores [14, 3, 22, 9, 17] in a list and prints how many of the numbers are greater than 10.