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.

Submitted: 0 / 10

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.