Standard Library

Python statistics Module

Python Statistical Functions

Python statistics module calculates mean and stdev.

Introduction to the statistics Module

The statistics module in Python provides a wide range of functions for calculating mathematical statistics of numeric data. It is part of the Python Standard Library and is available from Python 3.4 onwards. This module is especially useful for data analysis purposes, offering functions to compute mean, median, variance, and standard deviation among others.

Calculating the Mean

The mean (average) of a set of numbers is calculated using the mean() function from the statistics module. The mean is the sum of all the numbers divided by the count of numbers.

Here's how you can calculate the mean of a list of numbers:

Calculating the Standard Deviation

The standard deviation is a measure of the amount of variation or dispersion in a set of values. It can be calculated using the stdev() function in the statistics module. A low standard deviation indicates that the values tend to be close to the mean, whereas a high standard deviation indicates that the values are spread out over a wider range.

Below is an example of calculating the standard deviation:

Other Useful Functions in the statistics Module

Besides mean() and stdev(), the statistics module includes several other functions that are worth noting:

  • median(): Computes the median (middle value) of data.
  • mode(): Returns the most common data point.
  • variance(): Calculates the variance of the data, which is the squared standard deviation.
  • median_low() and median_high(): Return the low or high median of data, respectively.

These functions can be used similarly to the mean() and stdev() functions.

Previous
glob Module