Chapter 1 · Section 3 practice
NumPy Arrays
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. Create an array
Warm-up. Create an array from [10, 20, 30] and print it.
Write your answer, then run it.
2. Predict multiplication
Warm-up. Print values * 2 and its array equivalent. Explain the difference in a comment.
Write your answer, then run it.
3. Read attributes
Warm-up. Create a float array from [42, 58, 28]. Print shape and dtype.
Write your answer, then run it.
4. Adjust prices
Build your skills. Add HK$2 to all prices and print the updated array.
Write your answer, then run it.
5. Compute revenues
Build your skills. Calculate corresponding count × bill revenues and their total.
Write your answer, then run it.
6. Select high prices
Build your skills. Print the mask for prices >= 50 and the selected values.
Write your answer, then run it.
7. Calculate a proportion
Build your skills. Print the fraction of observed deliveries above 40 minutes, with two decimal places.
Write your answer, then run it.
8. Choose the convention
Challenge. Print the mean and sample standard deviation to three decimals.
Write your answer, then run it.
9. Measure percentage changes
Challenge. Use slices to compute successive percentage changes for [100, 110, 99]. Print rounded percentages.
Write your answer, then run it.
10. Revenue after a discount
Challenge. Reduce bills by 10%, keep counts fixed, and calculate revenues. Print dish revenues and total to two decimals. Explain the fixed-count assumption.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Create an array
import numpy as np
print(np.array([10, 20, 30]))Expected output
[10 20 30]np.array converts a list.
2. Predict multiplication
import numpy as np
values = [10, 20, 30]
print(values * 2)
print(np.array(values) * 2)
# The list repeats; the array doubles its values.Expected output
[10, 20, 30, 10, 20, 30]
[20 40 60]The array keeps three items; the repeated list has six.
3. Read attributes
import numpy as np
prices = np.array([42, 58, 28], dtype=float)
print(prices.shape)
print(prices.dtype)Expected output
(3,)
float64(3,) means three items in one dimension.
4. Adjust prices
import numpy as np
prices = np.array([42, 58, 28])
print(prices + 2)Expected output
[44 60 30]The scalar applies to every value.
5. Compute revenues
import numpy as np
counts = np.array([85, 110, 140])
bills = np.array([42, 58, 28])
revenue = counts * bills
print(revenue)
print(revenue.sum())Expected output
[3570 6380 3920]
13870Matching positions refer to matching dishes.
6. Select high prices
import numpy as np
prices = np.array([42, 58, 28, 65])
mask = prices >= 50
print(mask)
print(prices[mask])Expected output
[False True False True]
[58 65]A mask has one Boolean per item.
7. Calculate a proportion
import numpy as np
times = np.array([25, 42, 30, 45, 38])
print(f"{np.mean(times > 40):.2f}")Expected output
0.40Two of five observations exceed 40: 40%.
8. Choose the convention
import numpy as np
customers = np.array([100, 102, 98, 105, 101])
print(f"{customers.mean():.3f}")
print(f"{np.std(customers, ddof=1):.3f}")Expected output
101.200
2.588ddof=1 gives the sample standard deviation.
9. Measure percentage changes
import numpy as np
prices = np.array([100, 110, 99])
changes = (prices[1:] - prices[:-1]) / prices[:-1]
print(np.round(changes * 100, 1))Expected output
[ 10. -10.]Two adjacent comparisons give two percentage changes.
10. Revenue after a discount
import numpy as np
counts = np.array([85, 110, 140])
bills = np.array([42, 58, 28], dtype=float)
revenue = counts * (bills * 0.90)
print(np.round(revenue, 2))
print(f"{revenue.sum():.2f}")
# Demand is held fixed; actual counts may change.Expected output
[3213. 5742. 3528.]
12483.00This is an arithmetic scenario, not an estimated response of demand.