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…).
import math→ usemath.sqrt(25).import math as m→ usem.sqrt(25).from math import sqrt, pi→ usesqrt(25),pidirectly.from math import *→ loads all names (can clash with your own names; avoid).
help(math) or dir(math) lists what a module contains.
The math module
| Function | Example → 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, e | 3.14159…, 2.71828… |
The random module
random.random()→ a float with 0 ≤ x < 1.random.randint(a, b)→ an integer from a to b, both included.random.randrange(start, stop, step)→ an integer from range(start, stop, step), stop excluded. randrange(10) → 0 to 9.
Random number between 10 and 20 as a float: 10 + random.random() * 10. Outputs change every run.
The statistics module
statistics.mean(data)→ average (sum ÷ count).statistics.median(data)→ middle value after sorting; for an even count, the average of the two middle values.statistics.mode(data)→ the most frequent value.
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
- import m → m.f() · from m import f → f()
- ceil ↑, floor ↓; pow and sqrt give float
- randint(a, b): a..b included; randrange(a, b): b excluded
- random() → 0 ≤ x < 1
- mean = sum/n; median = middle; mode = most frequent
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
- Calling sqrt() after import math without writing math.sqrt().
- Thinking randint(1, 6) never gives 6. Both ends are included.
- Thinking randrange(1, 6) can give 6. The stop value is excluded.
- Expecting math.pow(2, 3) to give 8. It gives 8.0 (a float).