Chapter 0 · Section 4 practice
Lists and Repetition
Work from question 1 to 10. Write your code, click Run Code, review the result, then click Submit attempt. An attempt may be wrong: reference answers become available after you submit all 10. Each Python box is independent. Submission progress is saved in this browser.
1. Make and measure a list
Warm-up. Create scores containing 60, 75, and 90 in that order. Print the list, its number of items, and its total.
Write your answer, then run it.
2. Read and replace a score
Warm-up. Print the first and last scores using indexes. Replace the middle score with 85, then print the whole list.
Write your answer, then run it.
3. Add one score and several scores
Warm-up. Append 90 as one item, then extend scores with 85 and 95 as two separate items. Print the final flat list and its length.
Write your answer, then run it.
4. Insert, remove, and keep the removed value
Build your skills. Insert 75 at index 1. Remove the first value equal to 60. Pop the last item into removed. Print removed, then the remaining list.
Write your answer, then run it.
5. Search and sort a separate copy
Build your skills. Print the count of 75 and the first position of 75. Make a copy, sort the copy in ascending order, then print the sorted copy and the unchanged original.
Write your answer, then run it.
6. Visit and number five days
Build your skills. Use a for loop and range to print Day 1 through Day 5. Include day 5.
Write your answer, then run it.
7. Trace a total and calculate an average
Build your skills. Use a for loop to add each amount to a running total and print the total after each addition. After the loop, print the average using the current list length.
Write your answer, then run it.
8. Count down with a while loop
Challenge. Use while to print 3, 2, and 1 on separate lines, then print Done once. Update count inside the loop. Test count=0 afterwards: only Done should appear.
Write your answer, then run it.
9. Save until a target is reached
Challenge. Start at HK$45 and save HK$20 per day until the balance is at least HK$100. Use while, count days, and print each new balance with two decimals. Finally print Days: followed by the count. Test starting at 100 too.
Write your answer, then run it.
10. Process prices until the list is empty
Challenge. Use while and pop() to remove one price per repetition, adding it to a total. Print each running total with two decimals. After the loop, print the empty list and Total: HK$30.00. Test an empty starting list as well.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Make and measure a list
scores = [60, 75, 90]
print(scores)
print(len(scores))
print(sum(scores))Expected output
[60, 75, 90]
3
225The list preserves order; len counts items and sum adds numeric values.
2. Read and replace a score
scores = [60, 75, 90]
print(scores[0])
print(scores[-1])
scores[1] = 85
print(scores)Expected output
60
90
[60, 85, 90]Indexes start at zero. Assigning to index 1 replaces the second item without adding an item.
3. Add one score and several scores
scores = [60, 75]
scores.append(90)
scores.extend([85, 95])
print(scores)
print(len(scores))Expected output
[60, 75, 90, 85, 95]
5append adds one item. extend adds each item separately; appending [85, 95] would instead add one inner list.
4. Insert, remove, and keep the removed value
scores = [60, 90, 60, 85]
scores.insert(1, 75)
scores.remove(60)
removed = scores.pop()
print(removed)
print(scores)Expected output
85
[75, 90, 60]Insertion shifts items; removal deletes only the first 60. pop returns 85, while the other 60 remains.
5. Search and sort a separate copy
scores = [75, 60, 75, 90]
print(scores.count(75))
print(scores.index(75))
ordered = scores.copy()
ordered.sort()
print(ordered)
print(scores)Expected output
2
0
[60, 75, 75, 90]
[75, 60, 75, 90]There are two matches, with the first at index zero. Sorting a copy preserves the original list.
6. Visit and number five days
for day in range(1, 6):
print(f"Day {day}")Expected output
Day 1
Day 2
Day 3
Day 4
Day 5range excludes its stop, so use stop 6 to include day 5.
7. Trace a total and calculate an average
amounts = [10, 15, 5]
total = 0
for amount in amounts:
total = total + amount
print(total)
print(total / len(amounts))Expected output
10
25
30
10.0The running total is 10, 25, then 30. There are three items, so the average is 10.
8. Count down with a while loop
count = 3
while count > 0:
print(count)
count = count - 1
print("Done")Expected output
3
2
1
DoneThe test happens before each repetition. At zero it is false, so the loop body is skipped.
9. Save until a target is reached
balance = 45
daily_saving = 20
goal = 100
days = 0
while balance < goal:
balance = balance + daily_saving
days = days + 1
print(f"HK${balance:.2f}")
print("Days:", days)Expected output
HK$65.00
HK$85.00
HK$105.00
Days: 3The target can be exceeded. Three positive saving updates reach 105; a starting balance of 100 needs zero days.
10. Process prices until the list is empty
prices = [5, 10, 15]
total = 0
while len(prices) > 0:
price = prices.pop()
total = total + price
print(f"HK${total:.2f}")
print(prices)
print(f"Total: HK${total:.2f}")Expected output
HK$15.00
HK$25.00
HK$30.00
[]
Total: HK$30.00pop() removes the last item first: 15, 10, then 5. The length test prevents an empty pop; an initially empty list has total zero.