MCS-201 · Unit 12

Modules and Packages

Section 6 of 6

Block 3 · Unit 12Modules and Packages11 min · Section 6 of 6

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

NamePurposeExample
math.sqrt(x)Square rootmath.sqrt(16) = 4.0
math.pow(x, y)xyx^y, always as a floatmath.pow(2, 3) = 8.0 (whereas 2 ** 3 = 8)
math.floor(x), math.ceil(x)Largest integer ≤ x, smallest integer ≥ xmath.floor(2.7) = 2, math.ceil(2.1) = 3
math.pi, math.eConstants π and e3.141592653589793, 2.718281828459045
math.radians(x), math.degrees(x)Convert degrees to radians and backmath.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 bmath.log10(1000) = 3.0
math.factorial(n), math.gcd(a, b)n!, greatest common divisormath.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.)

FunctionReturns
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

FunctionMeaning
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)
statistics.pstdev(data), statistics.pvariance(data)Population versions (divide by n)

For data x1,…,xnx_1, \dots, x_n with mean xˉ\bar{x}:

s2=∑i=1n(xi−xˉ)2n−1,s=s2s^2 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1}, \qquad s = \sqrt{s^2}

Key takeaways

  • 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 1 · multiple choice

What does math.pow(3, 2) return?

Show answer

Answer: B. 9.0

math.pow always returns a float; 3 ** 2 would give the integer 9.

Question 2 · multiple choice

Which values can random.randint(1, 3) return?

Show answer

Answer: C. 1, 2 and 3

Both limits are included, unlike range(1, 3).

Question 3 · multiple choice

What does this program print?

Python · quiz_stats.py
import statistics
data = [3, 1, 4, 1, 5]
print(statistics.median(data), statistics.mode(data))
Show answer

Answer: B. 3 1

Sorted, the data is 1, 1, 3, 4, 5, so the median is 3; 1 occurs twice, so it is the mode. 2.8 is the mean.

Question 4 · multiple choice

What is math.floor(-1.2)?

Show answer

Answer: D. -2

floor gives the largest integer less than or equal to the value, which for −1.2 is −2.

Question 5 · multiple choice

Which call converts 180 degrees to radians?

Show answer

Answer: C. math.radians(180)

radians converts degrees to radians (giving π); degrees converts the other way.

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.

↑ ↓ move↵ openTab switch type