📘 CodingMarble Learn

Python Modules: math, random and statistics

A module is a Python file (.py) containing ready-made functions, constants and classes. You load it with import module_name (then call module.function()), import module as alias, or from module import name1, name2 (then call the name directly); from module import * loads everything (not recommended). The math module gives sqrt(), pow(), ceil(), floor(), fabs(), factorial(), gcd(), log(), log10(), sin(), cos(), tan() and constants pi and e. The random module gives random() (0 ≤ x < 1), randint(a, b) (a to b, both included) and randrange(start, stop, step) (stop excluded). The statistics module gives mean(), median() and mode().

🎬 Step-by-step story

  1. A module is a toolbox of ready-made functions. import math brings the whole box. Then you write math.sqrt(16).
  2. from math import sqrt takes out only that tool. Now you can write sqrt(16) without 'math.'.
  3. The math module has sqrt, pow, ceil, floor, fabs, factorial, gcd, and the value of pi.
  4. The random module gives surprise numbers. randint(1, 6) works like a dice.
  5. The statistics module finds the mean, the median and the mode of a list of numbers.
  6. Your turn: roll the dice many times and see the mean, median and mode.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why not have all functions ready without importing?

Loading everything would make every program slow and crowded with names. You import only the toolbox you need.

If I use from math import sqrt, can I still use math.pi?

No, the name math is not defined then. Import pi too, or use import math.

Why does math.pow give 8.0 but 2 ** 3 give 8?

math.pow always works with floats. The ** operator keeps ints as ints.

Are random numbers really random?

They are pseudo-random: made by a formula that looks random. That is fine for games and practice.

When is median better than mean?

When one value is very large or small. In [2, 3, 4, 100] the mean is 27.25 but the median is 3.5, which is more typical.

What is a module? import and from

A module is a .py file with functions and variables we can reuse. Python's standard library has many modules (math, random, statistics, datetime…).

help(math) or dir(math) lists what a module contains.

The math module

FunctionExample → result
sqrt(x)math.sqrt(49) → 7.0
pow(x, y)math.pow(2, 5) → 32.0 (always float)
ceil(x)math.ceil(4.1) → 5; ceil(-4.1) → -4
floor(x)math.floor(4.9) → 4; floor(-4.1) → -5
fabs(x)math.fabs(-3) → 3.0
factorial(n)math.factorial(5) → 120
gcd(a, b)math.gcd(12, 18) → 6
log(x, base), log10(x)math.log10(1000) → 3.0
sin(x), cos(x), tan(x)x in radians: math.sin(math.pi/2) → 1.0
pi, e3.14159…, 2.71828…

The random module

Random number between 10 and 20 as a float: 10 + random.random() * 10. Outputs change every run.

The statistics module

import statistics as st
d = [3, 5, 5, 2, 7]
print(st.mean(d), st.median(d), st.mode(d))   # 4.4 5 5

Board exam focus

Expect: output of math functions (ceil, floor, sqrt, pow), the possible outputs of a random-number statement (a favourite 2-mark question), import vs from, and mean/median/mode of a small list.

Key formulas and definitions

Worked examples

1. Output? import math; print(math.ceil(2.3), math.floor(-2.3), math.sqrt(81), math.pow(3, 2))

3 -3 9.0 9.0

2. Which of these can print(random.randint(3, 5)) show? 2, 3, 4, 5, 6

3, 4 or 5 (both ends included).

3. What are the possible outputs of random.randrange(1, 10, 3)?

range(1, 10, 3) = 1, 4, 7, so any one of 1, 4, 7.

4. Write a statement to get a random even number between 10 and 20 (both included).

random.randrange(10, 21, 2) → 10, 12, 14, 16, 18 or 20.

5. d = [4, 8, 6, 8, 2, 9]. Find mean, median and mode.

mean = 37/6 ≈ 6.17. Sorted [2, 4, 6, 8, 8, 9] → median = (6 + 8)/2 = 7.0. mode = 8.

6. Fix the error: from math import sqrt; print(math.sqrt(4))

With from-import, the name math is not defined. Write sqrt(4), or use import math.

7. Find the area of a circle of radius 7 using the math module.

import math r = 7 print(math.pi * r ** 2) # 153.938…

Common mistakes

Practice quiz

1. math.floor(7.9) is:
2. random.randint(1, 3) can return:
3. Which function gives the most frequent value?
4. After from math import pi, use pi as:
5. random.random() returns a value:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is a module in Python?

A .py file containing reusable functions and variables, loaded into a program with import or from … import.

What is the difference between randint() and randrange()?

randint(a, b) includes both a and b; randrange(start, stop, step) excludes stop and can skip values by step.

What does the statistics module do?

It gives functions like mean(), median() and mode() to find averages of numeric data.

Where this is taught

Canada (Ontario)Grade 12A. Programming Concepts and Skills
CBSE (India)Class 11Computational Thinking and Programming - 1

Learn first

Related lessons

All Computer Science lessons