Chapter 1 · Section 1 practice
Lists and Comprehensions
Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.
1. Read the first item
Warm-up. Print the first dish and the number of dishes.
Write your answer, then run it.
2. Select a range
Warm-up. Print the last dish, then a list containing only Char Siu and Toast.
Write your answer, then run it.
3. Add and remove
Warm-up. Append Toast; pop the first item. Print the removed item and remaining list.
Write your answer, then run it.
4. Raise every price
Build your skills. Use a comprehension to add HK$3 to every price. Print the new list.
Write your answer, then run it.
5. Keep affordable dishes
Build your skills. Keep prices strictly below HK$50 using a comprehension.
Write your answer, then run it.
6. Classify prices
Build your skills. Print one Premium label for prices at least 50, otherwise Regular.
Write your answer, then run it.
7. Calculate paired revenues
Build your skills. Use zip to pair counts with bills. Print revenues and their total.
Write your answer, then run it.
8. Check unequal inputs
Challenge. Print whether lengths match, then list(zip(counts, bills)). Explain the missing record in a comment.
Write your answer, then run it.
9. Filter and transform
Challenge. Apply a HK$5 discount only to original prices at least 50. Print discounted prices; exclude all others.
Write your answer, then run it.
10. Explain a price policy
Challenge. Raise each price by HK$2, label updated prices Premium at 50 or above, and print both lists. Explain why updated prices must be used.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Read the first item
dishes = ["Wonton", "Char Siu", "Milk Tea"]
print(dishes[0])
print(len(dishes))Expected output
Wonton
3Position 0 selects the first item. len counts items.
2. Select a range
dishes = ["Wonton", "Char Siu", "Toast", "Milk Tea"]
print(dishes[-1])
print(dishes[1:3])Expected output
Milk Tea
['Char Siu', 'Toast']The slice excludes position 3.
3. Add and remove
dishes = ["Wonton", "Char Siu"]
dishes.append("Toast")
removed = dishes.pop(0)
print(removed)
print(dishes)Expected output
Wonton
['Char Siu', 'Toast']pop(0) removes and returns the first item.
4. Raise every price
prices = [42, 58, 28]
print([price + 3 for price in prices])Expected output
[45, 61, 31]A transformation keeps one new value per original price.
5. Keep affordable dishes
prices = [42, 58, 28, 50]
print([price for price in prices if price < 50])Expected output
[42, 28]Strictly below excludes a price equal to 50.
6. Classify prices
prices = [42, 58, 28, 50]
print(["Premium" if p >= 50 else "Regular" for p in prices])Expected output
['Regular', 'Premium', 'Regular', 'Premium']Every price receives a label.
7. Calculate paired revenues
counts = [85, 110, 140]
bills = [42, 58, 28]
revenue = [c * b for c, b in zip(counts, bills)]
print(revenue)
print(sum(revenue))Expected output
[3570, 6380, 3920]
13870Each pair refers to one dish. The total is HK$13,870.
8. Check unequal inputs
counts = [85, 110, 140]
bills = [42, 58]
print(len(counts) == len(bills))
print(list(zip(counts, bills)))
# The last count has no matching bill.Expected output
False
[(85, 42), (110, 58)]False warns of incomplete corresponding records.
9. Filter and transform
prices = [42, 58, 28, 65]
print([p - 5 for p in prices if p >= 50])Expected output
[53, 60]The filter selects eligible items before computing their new prices.
10. Explain a price policy
prices = [48, 58, 28]
updated = [p + 2 for p in prices]
labels = ["Premium" if p >= 50 else "Regular" for p in updated]
print(updated)
print(labels)
# Classify updated prices; 48 becomes 50.Expected output
[50, 60, 30]
['Premium', 'Premium', 'Regular']The price at the boundary changes category after the increase.