You don't have to write everything from scratch. A module is a ready-made file of useful functions someone has already written — you just import it and use it. Python ships with a big standard library of them. This chapter covers how to import, and the three modules the syllabus expects: math, random and statistics.
1How to import
There are a few ways to bring a module's tools into your program:
| Form | Then you use… |
|---|---|
import math | math.sqrt(16) (module name prefix) |
from math import sqrt | sqrt(16) (just the name) |
import math as m | m.sqrt(16) (a short alias) |
import math is the safest and clearest — the math. prefix tells anyone reading your code exactly where sqrt comes from. Run the playground to see it work:Click Run to execute.
import maththen usemath.sqrt(...);from math import sqrtlets you usesqrt(...)directly.import math as mcreates a short alias.- The module-prefix form (math.sqrt) is clearest about where a function comes from.
2The math module
The math module provides mathematical constants and functions:
| Function / constant | Gives |
|---|---|
math.pi, math.e | The constants π (3.14159…) and e |
math.sqrt(x) | Square root |
math.pow(x, y) | x to the power y (as a float) |
math.ceil(x) | Round UP to the next whole number |
math.floor(x) | Round DOWN to the whole number below |
math.fabs(x) | Absolute value (always positive) |
It also has the trigonometry functions math.sin(), math.cos() and math.tan(). Try the playground:
Click Run to execute.
- Constants:
math.pi,math.e. sqrt,pow,fabs, and trig (sin/cos/tan).ceilrounds UP,floorrounds DOWN.
3random and statistics
The random module produces random numbers — perfect for games, simulations and shuffling:
random.random()— a float from 0.0 up to (not including) 1.0.random.randint(a, b)— a whole number from a to b inclusive (e.g. a dice roll).random.randrange(start, stop, step)— like range(), but picks one value.
Run it a few times — the output changes each run:
import statistics data = [10, 20, 20, 40] statistics.mean(data) # 22.5 (average) statistics.median(data) # 20 (middle value) statistics.mode(data) # 20 (most frequent)
The statistics module (shown above) gives mean() (average), median() (the middle value when sorted) and mode() (the most common value).
Click Run to execute.
- random.random() gives 0.0–1.0; randint(a,b) is inclusive of both ends; randrange works like range().
- statistics.mean = average, median = middle value, mode = most frequent.
- Random output differs on every run.
4Project: Scientific Calculator
Bring the math module together into a mini Scientific Calculator. Change num and run it — then add your own operations:
Click Run to execute.
- Modules let you build powerful tools quickly without reinventing the maths.
- math.sqrt, math.pow, math.ceil and math.floor cover common calculator needs.
- Combine modules with your earlier skills (variables, operators, conditions).
★ Project: extend the Scientific Calculator
Using the playground above:
- Add a line that prints math.pi and a circle's area for radius = num (area = pi × r × r).
- Use math.fabs() on a negative number and print the result.
- Import random and print a random integer 'lucky number' between 1 and 100.
- Bonus: try 'from math import sqrt' and call sqrt() without the math. prefix.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.