Stand on the standard library: import modules, pick single names with from-import, and treat your own .py files as modules too.
01
Learning objectives
State what a module is and why the standard library exists.
Use import math and call math.sqrt(), math.floor() and math.pi with dot notation.
Use from math import ... and explain how the naming differs from plain import.
Explain that your own .py file is a module importable by its file name.
Name common standard-library modules and the one-line job of each.
02
Lesson notes
Every useful program leans on code someone else already wrote and tested. Python ships this as modules — files of ready-made functions called the standard library. import math loads the maths module; its functions are then reached with dot notation: math.sqrt(144), math.floor(7.8), and the constant math.pi. The dot matters: the bare name sqrt is unknown until you import in the from-style.
from math import sqrt, ceil copies those names directly into your program, so you write sqrt(2) with no prefix — shorter, but riskier: if you define your own sqrt later, it silently replaces the imported one. The style rule: use import module for heavy use of a module, and from module import name when you need one or two specific names. Either way the code you call was tested by thousands of programmers before you — reach for it instead of rewriting it.
The tour worth memorising for the exam: math (roots, powers, floor, ceil, pi), statistics (mean, median, stdev — chapter 10 uses it in earnest), random (dice rolls and simulations), csv (spreadsheet-style data), and turtle (beginner drawings). Every one of these ships with Python itself, so nothing needs installing before your first import. You are not expected to memorise every function — knowing which module to reach for is the skill being tested.
Your own files are modules too. Save your helper functions in utils.py; in another file in the same folder, write import utils and call utils.cm_to_inch(254). This is how a project grows: one file per concern, sharing code through imports instead of copy-paste. For the exam, remember that a module is imported by its file name without the .py part, and the file must sit in the same folder (or on the module path).
Board tip: MCQs check exact spelling — import math then math.sqrt(x), or from math import sqrt then plain sqrt(x). Mixing the two styles (importing math but calling a bare sqrt(x)) raises NameError, a favourite distractor. When an MCQ shows a bare call, ask first: was this name imported with from, or only through the module name? Note that our worked examples avoid random on purpose, so every output shown here is reproducible when you revise.
03
Worked code examples
import and dot notation
The module name prefixes every function. sqrt always returns a float, pi is a stored constant, and floor chops 7.8 down to the whole number 7.
import math
print(math.sqrt(144))
print(math.pi)
print(math.floor(7.8))
Output
12.0
3.141592653589793
7
from-import copies names in
The imported names are used bare, without the math. prefix; ceil rounds 3.2 up to 4, the opposite of floor.
from math import sqrt, ceil
print(sqrt(2))
print(ceil(3.2))
Output
1.4142135623730951
4
A tiny calculation with a module constant
math.pi keeps full precision through the calculation; round is applied once, only at reporting time.
import math
r = 7
area = math.pi * r * r
print(round(area, 2))
Output
153.94
04
Key terminology
module
A file of ready-made functions and constants that can be imported by name.
standard library
The large collection of modules that ships with Python itself.
import
The statement that loads a module, e.g. import math.
from-import
Importing specific names directly, e.g. from math import sqrt.
dot notation
Reaching a module's members with a dot, as in math.sqrt(144).
05
Common misconceptions
Common mistake
import math runs all of math's functions immediately.
Correct understanding
Importing just makes the module's names available. Its functions run only when you call them, like math.sqrt(144).
Common mistake
After from math import sqrt, you can also write math.sqrt(x).
Correct understanding
The from-style imports only the named function; the name math itself is not defined unless you also import math.
Common mistake
Calling bare sqrt(x) works as long as math was imported at the top.
Correct understanding
import math requires the prefix math.sqrt(x); without from math import sqrt, the bare name raises NameError.
06
Check your understanding
1. After import math, which line prints the square root of 144?
2. After from math import floor, what is floor(3.9)?
3. Your helper functions live in utils.py in the same folder. How do you use them from main.py?