12.6Standard Library Modules: math, random and statistics
Covers IGNOU MCS-201 §12.6
Three standard modules handle most everyday numerical work: math for mathematical functions, random for random numbers and choices, and statistics for summarising data.
The math module
Name
Purpose
Example
math.sqrt(x)
Square root
math.sqrt(16) = 4.0
math.pow(x, y)
xy, always as a float
math.pow(2, 3) = 8.0 (whereas 2 ** 3 = 8)
math.floor(x), math.ceil(x)
Largest integer ≤ x, smallest integer ≥ x
math.floor(2.7) = 2, math.ceil(2.1) = 3
math.pi, math.e
Constants π and e
3.141592653589793, 2.718281828459045
math.radians(x), math.degrees(x)
Convert degrees to radians and back
math.degrees(math.pi) = 180.0
math.sin(x), math.cos(x), math.tan(x)
Trigonometric functions (x in radians)
math.sin(math.pi / 2) = 1.0
math.log(x), math.log10(x), math.log(x, b)
Natural log, base-10 log, log to base b
math.log10(1000) = 3.0
math.factorial(n), math.gcd(a, b)
n!, greatest common divisor
math.factorial(5) = 120
The random module
random produces pseudo-random numbers: they look random but come from a deterministic algorithm. random.seed(n) fixes the starting point so the same sequence appears every run, which is useful for testing. (For passwords and security tokens, use the secrets module instead.)
Function
Returns
random.random()
A float in the range [0.0, 1.0)
random.randint(a, b)
An integer from a to b, both included
random.choice(seq)
One random item of a sequence
random.shuffle(lst)
Shuffles a list in place (returns None)
random.sample(seq, k)
k distinct items chosen at random
The statistics module
Function
Meaning
statistics.mean(data)
Arithmetic mean (average)
statistics.median(data)
Middle value of the sorted data (average of the two middle values for an even count)
statistics.mode(data)
Most common value
statistics.stdev(data), statistics.variance(data)
Sample standard deviation and variance (divide by n − 1)
math: sqrt, pow, floor, ceil, trigonometry in radians, logs, pi, e.
random: random, randint (inclusive), choice, shuffle, sample; seed for repeatability.
statistics: mean, median, mode, stdev/variance (sample) and pstdev/pvariance (population).
Module and function names are lowercase and case-sensitive.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Calculate the mean and the sample standard deviation of 10, 12, 14 by hand, and write the statistics calls that check them.
Show worked answer
Mean = (10 + 12 + 14) / 3 = 36 / 3 = 12. Deviations from 12 are −2, 0, 2; squared: 4, 0, 4; sum = 8. Sample variance = 8 / (3 − 1) = 4, so the sample standard deviation = √4 = 2. Check: import statistics, statistics.mean([10, 12, 14]) gives 12 and statistics.stdev([10, 12, 14]) gives 2.0.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/modules-and-packages/standard-library-modules-math-random-and-statistics/ to read this page.