Learn by reading through in order

math and statistics — Single-Value Math and Data Aggregation

Learn Python's math and statistics modules from the ground up. Covers single-value math with math.sqrt / pi / floor / ceil / log, data aggregation with statistics.mean / median / mode / stdev, and how the two modules split responsibilities — with runnable practice exercises.

This article focuses on Python's numeric modules for the everyday case where regular `float` is good enough. math handles operations on a single value (square roots, logarithms, trig functions); statistics handles aggregating a sequence of numbers (mean, median, standard deviation). When you need to avoid floating-point error strictly, the next article on decimal / fractions is the right place.

How math and statistics Split Responsibilities
mathsqrt / floor / pistatisticsmean / median / stdevInput: one number(int / float)Input: list of numbers[1200, 1500, ...]
math transforms a single number; statistics summarizes a list of numbers. The shape of the input is what tells you which module to reach for.

math — Operations on a Single Number

math is the standard library for operations on a single number. math.sqrt for square roots, math.floor / math.ceil for rounding down / up, math.log for logarithms, math.sin / math.cos for trig — the textbook math functions are all there. It also exposes constants as attributes like math.pi and math.e (pi and the base of the natural log).

The main math Functions
math.sqrt(x)square rootmath.floor(x)round downmath.ceil(x)round upmath.log(x, base)logarithmmath.pipi (constant)math.sin(x)math.cos(x)trig functions
math.sqrt(x) is the square root, math.floor(x) / math.ceil(x) round down / up, math.log(x, base) is the logarithm. math.pi and math.e are values themselves (attributes, not functions).
Function / AttributeMeaningExample
math.sqrt(x)Square root (returns float)math.sqrt(16) → 4.0
math.floor(x)Round down (returns int)math.floor(3.7) → 3
math.ceil(x)Round up (returns int)math.ceil(3.2) → 4
math.log(x, base)Logarithm (natural log when base is omitted)math.log(100, 10) → 2.0
math.piPi (constant)3.141592653589793
math.eBase of natural log (constant)2.718281828459045
import math

print(math.sqrt(16))         # 4.0  (float)
print(math.floor(3.7))       # 3    (int)
print(math.ceil(3.2))        # 4    (int)
print(math.log(100, 10))     # 2.0
print(math.pi)               # 3.141592653589793
print(round(math.pi, 4))     # 3.1416  (rounded to 4 decimal places)

Try the most common math functions and constants.

① Import math.

② Compute the square root of 16 with math.sqrt and print it as sqrt(16): ◯.◯.

③ Compute the floor of 3.7 and the ceil of 3.2 with math.floor / math.ceil, and print them as floor: ◯ ceil: ◯.

④ Round math.pi to 4 decimal places and print it as pi (4 dp): ◯.◯◯◯◯.

(If your code runs correctly, the explanation will appear.)

Python Editor

Run code to see output

statistics — Aggregating and Summarizing Data

statistics is the standard library for aggregating a group of values. statistics.mean returns the arithmetic mean, statistics.median the median, statistics.mode the most frequent value, and statistics.stdev the sample standard deviation — all in a single call. You can also see classic statistical observations in action, like median being a more stable summary than the mean when outliers are present, with just a few lines of code.

The Main statistics Functions
[1200, 1500,1100, 1400]list of numbersstatistics.mean→ averagestatistics.median→ medianstatistics.stdev→ std deviation
statistics.mean is the average, statistics.median is the median, statistics.mode is the most common value, statistics.stdev is the standard deviation. They all take a list of numbers and return one number.
FunctionMeaningExample
statistics.mean(d)Arithmetic meanmean([1,2,3]) → 2
statistics.median(d)Median (resists outliers)median([1,2,3,4]) → 2.5
statistics.mode(d)Mode (most frequent value)mode([1,2,2,3]) → 2
statistics.stdev(d)Sample standard deviationHow spread out the values are
import statistics

prices = [1200, 1500, 1100, 1400]

print(statistics.mean(prices))    # 1300       (mean)
print(statistics.median(prices))  # 1300.0     (median)
print(statistics.stdev(prices))   # ~182.57    (standard deviation)

# When there's an outlier, mean and median diverge
with_outlier = [1200, 1500, 1100, 1400, 100000]
print(statistics.mean(with_outlier))    # 21040  (pulled by the outlier)
print(statistics.median(with_outlier))  # 1400   (resists the outlier)

Compute the mean, median, and standard deviation of four product prices.

① Import statistics and put the four prices [1200, 1500, 1100, 1400] in a list called prices.

② Compute the mean with statistics.mean, round it to an integer, and print it as mean: ◯.

③ Compute the median with statistics.median, round it to an integer, and print it as median: ◯.

④ Compute the standard deviation with statistics.stdev, round it to 1 decimal place, and print it as stdev: ◯.◯.

Python Editor

Run code to see output
QUIZ

Knowledge Check

Answer each question one by one.

Q1Which is the right call to compute the square root of a single number?

Q2Which is the most appropriate way to compute the mean of a list?

Q3Which one returns the outlier-resistant median?