Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s built-in statistics module provides straightforward tools for summarizing data: averages and medians for central location, variance and standard deviation for spread, and quantiles for cut points. Here are ten useful functions, grouped by purpose—not a complete list. Python 3.14.8 documents additional functions, including covariance, correlation, and linear regression.

Before choosing a function

First decide what your data represents. A sample is a subset used to estimate a larger group; a population is the entire group you want to describe. That distinction determines which variance and standard-deviation functions to use.

Most functions in the module support int, float, Decimal, and Fraction values. Avoid mixing numeric types in one dataset: behavior for mixed-type collections is undefined and implementation-dependent. Remove NaNs before calling functions that sort values or count occurrences, such as median(), mode(), and quantiles().

The examples use the official Python 3.14.8 statistics documentation as their reference. geometric_mean() and quantiles() were added in Python 3.8; weighted harmonic_mean() was added in Python 3.10. In Python 3.13, quantiles() changed to accept a dataset containing a single data point.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Central location: where is the center?

1. mean(): arithmetic average

mean() adds the values and divides by their count. It works with a sequence or iterable, and raises StatisticsError for empty input.

from statistics import mean

scores = [72, 81, 86, 91]
print(mean(scores))  # 82.5

The arithmetic mean is useful when every observation should contribute proportionally, but an extreme value can pull it away from what is typical. For example, a few unusually high incomes can make a group’s mean income much higher than most individuals’ incomes.

For exact arithmetic, the function can preserve supported numeric types such as Decimal and Fraction:

from fractions import Fraction
from statistics import mean

print(mean([Fraction(1, 3), Fraction(2, 3)]))  # Fraction(1, 2)

2. median(): middle value, or midpoint of the two middle values

median() sorts the data and returns the middle value. When the dataset has an even number of values, it returns the mean of the two middle values; that result need not be an observed value. Because extreme values do not pull it as strongly as they pull the mean, the median can better represent a skewed dataset.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
from statistics import median

print(median([1, 3, 4, 100]))  # 3.5

For the same sorted values, median_low() and median_high() return the lower or higher middle observation instead. Those are useful when the result must be an actual data point, including for suitable ordinal data such as ranked categories.

3. mode(): one most-common value

mode() returns the most frequently occurring value. It can work with nominal data—categories without a numeric scale—such as color names. If values tie for highest frequency, it returns the first tied value encountered in the input.

from statistics import mode

print(mode(["blue", "red", "blue", "red", "green"]))  # blue

4. multimode(): every most-common value

Use multimode() when you need all values tied for the highest frequency. It returns them in encounter order, so the example below returns both colors.

from statistics import multimode

print(multimode(["blue", "red", "blue", "red", "green"]))  # ['blue', 'red']

Specialized averages: when values are rates or multiply together

5. geometric_mean(): multiplicative average

The geometric mean is useful for values that combine multiplicatively, such as growth factors across periods. Unlike the arithmetic mean, it does not average by simple addition. Python converts inputs to floats; the function rejects empty data and any value that is zero or negative.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
from statistics import geometric_mean

print(geometric_mean([1.1, 1.21]))

Use it only when the data and question make a multiplicative average appropriate; it is not a drop-in replacement for the arithmetic mean.

6. harmonic_mean(): average for rates and ratios

The harmonic mean is often appropriate for averaging rates or ratios. The Python documentation gives speed as an example: averaging speeds over equal distances is different from taking their arithmetic mean.

from statistics import harmonic_mean

print(harmonic_mean([40, 60]))

Python also supports weighted harmonic means; that capability was added in Python 3.10. Supply weights when observations represent different amounts of the quantity being averaged, and make sure the weights match the data in length and meaning.

Spread: how much do observations vary?

7. variance(): sample variance

Use variance() when your data is a sample and you want to estimate variance in a larger population. It uses N − 1 degrees of freedom, where N is the sample size, and requires at least two data points.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
from statistics import variance

measurements = [2, 4, 6, 8]
print(variance(measurements))

An optional xbar argument lets you supply the sample mean, but Python does not check that your value is correct. A mistaken xbar can therefore produce a misleading result.

8. pvariance(): population variance

Use pvariance() when your dataset contains the whole population of interest, rather than a sample. It divides by N, not N − 1.

from statistics import pvariance

entire_group = [2, 4, 6, 8]
print(pvariance(entire_group))

Do not choose between variance() and pvariance() based on which result looks preferable. Choose according to whether the observations are a sample or the full population.

9. stdev(): sample standard deviation

stdev() is the square root of the sample variance. Like variance(), it treats the data as a sample and requires at least two values. Because it is expressed in the same units as the original measurements, it is often easier to interpret than variance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
from statistics import stdev

print(stdev([2, 4, 6, 8]))

10. pstdev(): population standard deviation

pstdev() is the square root of the population variance. Use it for a complete population, not a sample.

from statistics import pstdev

print(pstdev([2, 4, 6, 8]))
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Bonus: divide data into quantiles

quantiles() is an additional useful function, not included in the ten above. It returns cut points that divide ordered data into a chosen number of intervals. By default, n=4 returns quartile cut points, using the exclusive method.

from statistics import quantiles

values = [1, 2, 3, 4, 5, 6, 7, 8]
print(quantiles(values, n=4, method="exclusive"))

The method matters: exclusive and inclusive can produce different cut points. The inclusive method treats the observed minimum and maximum as the 0th and 100th percentiles. State which method you use when reporting results rather than treating quantile cut points as method-independent.

Where the built-in module fits

The statistics module is suitable for basic statistical calculations directly in Python. The Python documentation says: “The module is not intended to be a competitor to third-party libraries such as NumPy, SciPy, or proprietary full-featured statistics packages aimed at professional statisticians such as Minitab, SAS and Matlab.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If your question is about relationships between variables rather than a single dataset’s center or spread, the module also documents covariance(), correlation(), and linear_regression(). They are outside this selected introduction, but are part of the broader function set.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.