Q1Which is the right call to compute the square root of a single number?
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.
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).
math.pi and math.e are values themselves (attributes, not functions).| Function / Attribute | Meaning | Example |
|---|---|---|
| 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.pi | Pi (constant) | 3.141592653589793 |
| math.e | Base 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)
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.
| Function | Meaning | Example |
|---|---|---|
| statistics.mean(d) | Arithmetic mean | mean([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 deviation | How 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)
Knowledge Check
Answer each question one by one.
Q2Which is the most appropriate way to compute the mean of a list?
Q3Which one returns the outlier-resistant median?