Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Reductions and statistics

Reduce a finite sample to a total or a statistic suited to the question.

Version: Python 3.14; repository image, production runtime not attested

Syntax

statistics.median(samples)

Reduce a finite sample to a total or a statistic suited to the question.

The arithmetic mean depends on every magnitude, while the median uses ordered position and for even-size numeric data averages the two middle values. Sum has an additive identity and returns zero for empty input.

Mean and median instead raise StatisticsError for an empty sample.

Examples

from statistics import mean, median, StatisticsError
data = [1, 2, 3, 94]
print(sum(data), mean(data), median(data), sum([]))
for function in (mean, median):
    try:
        function([])
    except StatisticsError as error:
        print(function.__name__, type(error).__name__)

Result

100 25 2.5 0
mean StatisticsError
median StatisticsError

Mean and median instead raise StatisticsError for an empty sample.

Sources