Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

statistics

Python’s statistics module provides standard-library functions for averages, central tendency, spread, relationships, quantiles, and normal distributions over numeric data.

Since
3.4

How it works

The module supplies functions for common statistics on numeric data, including averages, central values, spread, relationships, and probability distributions. Most functions accept an iterable, while specific functions distinguish population calculations from sample calculations.

Syntax

import statistics
statistics.mean(data)

Parameters

NameTypeDescription
dataiterable of Real-valued numbersAn iterable or sequence of numeric values; individual functions may also accept a reference mean, population mean, weights, or related options.
xbarReal, optionalOptional precomputed sample mean for functions such as variance() and stdev().
muReal, optionalOptional population mean for functions such as pvariance() and pstdev().

Returns

The selected statistical function returns its documented numeric result or distribution-related object; exact return type depends on the function.

Key points

  • statistics offers standard-library calculations for numeric data without requiring a third-party package.
  • mean() computes the arithmetic average.
  • fmean() is a faster floating-point mean and can incorporate weights.
  • median() finds the central value, averaging the two central values for an even-sized input.

Common mistakes

  • Use stdev() for sample spread; it is not the population standard deviation.
  • variance() needs at least two real-valued observations.
  • The module is intended for numeric Real-valued data rather than arbitrary objects.

Example: Summarize numeric data

import statistics

values = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
print(statistics.mean(values))
print(statistics.median(values))
print(statistics.variance(values))

Computes an arithmetic mean, median, and sample variance for numeric data.

When to use it

Descriptive summaries

Summarize measurements, timings, scores, or other numeric observations.

Spread analysis

Compare variation in a sample or an entire population.

Quantiles and relationships

Estimate distribution boundaries and relationships between paired observations.

Normal-distribution work

Model or manipulate a normal distribution.

Interview questions

QWhat does statistics.mean() compute?

mean(data) returns the sample arithmetic mean: the sum of the values divided by their count. It accepts a sequence or iterable of real-valued numbers.

Follow-up: How would you calculate the population spread? Use pvariance() and pstdev(), rather than the sample-oriented variance() and stdev().

QHow does statistics.median() handle an even number of values?

median(data) returns the middle value using the common method that averages the two central values when the data count is even. It operates on numeric data.

Follow-up: Which function preserves the lower or upper central value instead? Use median_low() or median_high().

QWhat is the distinction between variance() and pvariance()?

variance(data, xbar=None) returns sample variance and requires an iterable containing at least two real-valued numbers. Passing xbar can provide a precomputed mean.

Follow-up: What corresponding function computes population variance? Use pvariance(data, mu=None), optionally supplying the population mean as mu.

  • mean
  • fmean
  • geometric_mean
  • harmonic_mean
  • median
  • median_low
  • median_high
  • median_grouped

Source: statistics — Mathematical statistics functions. Checked 2026-10-16.