statistics

Basic statistics without external libraries: mean, median, mode, standard deviation, and quantiles.

On this page

Example

#
Python
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))
Resultsaved result, you can check it
78.4
72
72
16.0

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

#
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 an int when the result comes out whole.
  • fmean()

    mean

    A faster mean, always returns a float. 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, use pstdev().
  • variance()

    variance

    The sample variance, that is the square of the standard deviation. For the whole group, use pvariance().
  • quantiles()

    quantiles

    The points dividing the data into n equal parts, quartiles by default (n=4). Returns a list.

More examples

#
Mean versus median with an extreme value
Python
import statistics

minutes = [25, 30, 20, 35, 30, 240]

print(round(statistics.mean(minutes), 1))
print(statistics.median(minutes))
Resultsaved result, you can check it
63.3
30.0

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.

The same mean, a different spread
Python
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))
Resultsaved result, you can check it
74 74
3.2 19.0
[56.0, 74.0, 92.0]

mean() returns an int when the mean of integers comes out whole, which is why you see 74, not 74.0.

Modes and an empty list
Python
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)
Resultsaved result, you can check it
B
['B', 'A']
mean requires at least one data point

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 than mean().
  • stdev() computes the sample deviation, that is it divides by n - 1. When you have data about the whole group, not just a sample, use pstdev().

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.