statistics
Basic statistics without external libraries: mean, median, mode, standard deviation, and quantiles.
Example
#import statistics
scores = [72, 95, 64, 88, 72, 100, 58]
print(round(statistics.mean(scores), 1))
print(statistics.median(scores))
print(statistics.mode(scores))
print(round(statistics.stdev(scores), 1))Definition and usage
#The statistics module computes basic statistical measures for lists of numbers, without installing pandas or NumPy. The mean() function returns the arithmetic mean, median() the median, meaning the middle value, and mode() the mode, meaning the most frequent value. That is enough to analyze test scores, study time, or the number of tasks solved.
The median is resistant to extreme values. One very high result raises the mean a lot and changes the median very little, so for times, prices, or earnings it is worth looking at both measures. With an even number of data points, median() returns the average of the two middle values.
The spread of data is described by stdev() and variance(), that is the standard deviation and the variance. Two groups can have the same mean and a completely different spread of results. quantiles() divides the data into equal parts and returns the dividing points, e.g. quartiles. An empty list raises StatisticsError, and a sample standard deviation requires at least two values.
Syntax
#import statistics
statistics.mean(data)
statistics.median(data)
statistics.mode(data)
statistics.stdev(data)
statistics.quantiles(data, n=4)The most important functions
#mean()
mean
The arithmetic mean. For integers, it returns anintwhen the result comes out whole.fmean()
mean
A faster mean, always returns afloat. Since Python 3.11 it also accepts weights (weights).median()
median
The middle value after sorting. With an even number of data points, the average of the two middle ones.mode()
mode
The most frequent value. In a tie, it returns the one that appeared first. It also works on strings.multimode()
modes
A list of all the most frequent values, in order of first appearance.stdev()
deviation
The sample standard deviation. For data about the whole group, usepstdev().variance()
variance
The sample variance, that is the square of the standard deviation. For the whole group, usepvariance().quantiles()
quantiles
The points dividing the data intonequal parts, quartiles by default (n=4). Returns a list.
More examples
#import statistics
minutes = [25, 30, 20, 35, 30, 240]
print(round(statistics.mean(minutes), 1))
print(statistics.median(minutes))One four-hour session more than doubled the mean, while the median still shows the typical study time. With an even number of data points the median is the average of the two middle values, hence 30.0.
import statistics
group_a = [70, 72, 74, 76, 78]
group_b = [50, 62, 74, 86, 98]
print(statistics.mean(group_a), statistics.mean(group_b))
print(round(statistics.stdev(group_a), 1), round(statistics.stdev(group_b), 1))
print(statistics.quantiles(group_b, n=4))mean() returns an int when the mean of integers comes out whole, which is why you see 74, not 74.0.
import statistics
answers = ["B", "A", "B", "C", "A"]
print(statistics.mode(answers))
print(statistics.multimode(answers))
try:
statistics.mean([])
except statistics.StatisticsError as error:
print(error)Good practices
#- With data that has extreme values, such as times, prices, or earnings, give the median next to the mean. It reflects the typical value better.
- For large lists of floating-point numbers,
fmean()is noticeably faster thanmean(). stdev()computes the sample deviation, that is it divides byn - 1. When you have data about the whole group, not just a sample, usepstdev().
Related entries
#- mathA module of math functions: square roots, rounding, powers, logarithms, and constants.
- sum()Adds together all the numbers in a collection.
- round()Rounds a number to the given number of decimal places.
- collections.CounterA dictionary for counting: for each item, it stores the number of times it occurs.
- sorted()Returns a new, sorted list built from the items of any collection.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.