Sub topic 1
Start by Ordering the Data
Prerequisites
This chapter assumes you can order numbers from least to greatest, find the mean and median of a list, and compute with fractions, decimals, and percents. The code uses Python 3.
Why Order the Data First
Most position and spread measures depend on sorted data. Sort the values from smallest to largest before you find a median, range, or quartile. Unsorted data hides the positions you need.
The Median as the Middle Value
The median is the middle value of an ordered list. For an odd count, it is the single middle entry. For an even count, it is the mean of the two middle entries.
| Data set | Count | Median |
|---|---|---|
| 3, 5, 7, 8, 12 | 5 | 7 |
| 3, 5, 7, 8, 12, 13 | 6 | 7.5 |
Range: The Simplest Measure of Spread
The range is the largest value minus the smallest value. It shows how far apart the extremes are. It ignores every value in between, so one extreme value can change it sharply.
Quartiles: Splitting the Data into Four Parts
Quartiles divide ordered data into four parts, each holding about a quarter of the values. Q2 is the median. Q1 is the median of the lower half, and Q3 is the median of the upper half. When the count is odd, leave the middle value out of both halves.
def median(values):
n = len(values)
mid = n // 2
if n % 2 == 1:
return values[mid]
return (values[mid - 1] + values[mid]) / 2
def quartiles(data):
s = sorted(data)
half = len(s) // 2
lower = s[:half]
upper = s[half + len(s) % 2:]
return median(lower), median(s), median(upper)
data = [21, 3, 12, 8, 14, 5, 18, 7, 13]
q1, q2, q3 = quartiles(data)
print(q1, q2, q3) # 6.0 12 16.0
print(max(data) - min(data)) # 18Exercise
For the data 4, 9, 2, 11, 6, 15, 8, what are Q1, Q2, and Q3, and what is the range?
Show answer
Sorted, the data is 2, 4, 6, 8, 9, 11, 15. Q2 is 8. The lower half 2, 4, 6 has median Q1 = 4. The upper half 9, 11, 15 has median Q3 = 11. The range is 15 - 2 = 13.