WEB PRACTICE CHAPTER 05

Lists and Data

Ten additional coding tasks · W041–W050

These extra website exercises are not a substitute for the book's exact numbered questions. Follow your printed book separately using the 100-challenge tracker.
Chapter 5 code

Choose starters to try yourself, or download a separate worked example pack after attempting the tasks.

Chapter 5 starters ↓Chapter 5 worked examples ↓
WEB PRACTICE W041

Largest score

Find the largest of five test scores.

Reveal a hint

Use max() or a loop that tracks the largest.

Reveal example code (after trying it yourself)
scores = [12, 18, 9, 23, 16]
print("Highest:", max(scores))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W042

Mean score

Calculate the mean of five quiz scores.

Reveal a hint

Divide sum(scores) by len(scores).

Reveal example code (after trying it yourself)
scores = [5, 8, 9, 7, 6]
print("Mean:", sum(scores) / len(scores))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W043

Above threshold

Find all measurements greater than 20.

Reveal a hint

Use a list comprehension with an if condition.

Reveal example code (after trying it yourself)
measures = [18, 21, 25, 13, 22]
above = [x for x in measures if x > 20]
print(above)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W044

Remove duplicates

Remove duplicate names while retaining their first-seen order.

Reveal a hint

Append only when an item is not already in the result.

Reveal example code (after trying it yourself)
names = ["Ada", "Leo", "Ada", "Mia", "Leo"]
unique = []
for name in names:
    if name not in unique:
        unique.append(name)
print(unique)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W045

Sort temperatures

Sort a list of temperatures from coldest to warmest.

Reveal a hint

sorted() returns a new list.

Reveal example code (after trying it yourself)
temperatures = [19, 12, 25, 18, 15]
print(sorted(temperatures))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W046

Running total

Create cumulative totals from weekly savings.

Reveal a hint

Append a fresh running total after each amount.

Reveal example code (after trying it yourself)
savings = [3, 2, 5, 4]
running = []
total = 0
for amount in savings:
    total += amount
    running.append(total)
print(running)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W047

List filtering

Keep only the even numbers from a list.

Reveal a hint

Use n % 2 == 0.

Reveal example code (after trying it yourself)
values = [1, 4, 7, 12, 15, 20]
print([n for n in values if n % 2 == 0])

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W048

Find position

Find the index of the word blue in a list of colours.

Reveal a hint

List indices start from zero.

Reveal example code (after trying it yourself)
colours = ["red", "green", "blue", "yellow"]
print("Index:", colours.index("blue"))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W049

Pair two lists

Combine names and scores into a series of readable results.

Reveal a hint

zip() pairs corresponding items.

Reveal example code (after trying it yourself)
names = ["Ada", "Ben", "Cara"]
scores = [9, 6, 8]
for name, score in zip(names, scores):
    print(f"{name}: {score}")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W050

Reverse a list

Display a list of tasks in reverse order without changing the original.

Reveal a hint

Slice with [::-1].

Reveal example code (after trying it yourself)
tasks = ["plan", "code", "test", "review"]
print(tasks[::-1])

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.