statistics.mean, statistics.median
statistics.mean(data)
statistics.median(data)Compute the arithmetic mean or the median of a sequence of numeric data.
| Parameter | Description |
|---|---|
data | An iterable of numbers. It is fully consumed. An empty input is an error for both of these functions. |
Returns: A number. For a list of ints, mean and an even-length median often return a float, as in median([1, 2, 3, 4]) == 2.5.
Raises
statistics.StatisticsError— mean([]) raises StatisticsError: mean requires at least one data point.
- These helpers do not ignore non-finite values for you. If the data may contain NaN, decide how to handle it before calling them.
import statistics
print(statistics.mean([1, 2, 3, 4]))
print(statistics.median([1, 2, 3, 4]), statistics.median([1, 2, 3]))
try:
statistics.mean([])
except statistics.StatisticsError as exc:
print(f"{type(exc).__name__}: {exc}")Expected output
2.5
2.5 2
StatisticsError: mean requires at least one data point