Modules and the standard library
Other people’s code, one line away: importing from the standard library that ships with Python — math, random and friends.
Other people’s code, one line away
A module is a file of Python someone else has written, and import makes its names yours. The ones that ship with Python are called the standard library — hundreds of modules for maths, dates, randomness, statistics and more, already installed wherever Python runs.
import math puts the module in your program under its own name, and everything inside it is reached through a dot: math.sqrt(144) is the square root function, borrowed in one line.
Two modules, four lines
import random
import math
random.seed(7)
print(random.randint(1, 6))
print(math.sqrt(144))Same seed, same numbers
random is deliberately unpredictable: run random.randint(1, 6) twice and you get two rolls. That is what a dice roll should be, and it is also what makes a program hard to teach from.
random.seed(7) fixes the starting point of the sequence. With a seed, the same rolls come out every run — repeatable for a lesson, and for a test that needs the same answer twice. Without one, the numbers are fresh each time, which is usually what you want in a game.
from … import …
If you only need one name, from math import sqrt brings just that function in, and it is called as plain sqrt(144). Shorter to read, slightly harder to trace: a bare sqrt tells the reader less about where it came from than math.sqrt does.
Keep imports at the top of the file, where the reader can see what the program depends on before reading what it does. Python does not require it; every reader does.
Five rolls and a square root
Roll the dice five times with a for loop, then use the math module to print the square root of 144 on a line of its own.
The module name is a name like any other
import math binds math in your file. Nothing stops a later line from rebinding it, so the name means whatever it most recently pointed at. The dot is a lookup on that object.
That is also why from math import sqrt can be shadowed by a variable called sqrt further down. Python will not warn you; the name simply means something else from that line onward. Keeping imports at the top and names specific makes the collision unlikely.
The standard library is part of the language
A module from the standard library is not fetched when you import it. It is already installed with the interpreter, which is why the playground can offer math and random without a network. An import costs a lookup, not a download, and the documentation describes behaviour guaranteed across versions rather than one implementation.
That guarantee is the reason to prefer it over a hand-written helper for ordinary work: statistics.mean handles an empty list in a defined way, and datetime knows about leap years. Writing those yourself is a good exercise and a poor default. Read the function’s documentation before reimplementing it; the edge cases are usually the point.
The standard library is documented at docs.python.org — math, random, datetime, statistics and hundreds more, none of them needing an install.
A module is a namespace
Importing does not pour every name into your file. It binds one name — the module — and everything else waits behind a dot. That is why math.sqrt and random.randint can both exist without arguing, and why a reader can tell where a name came from.
import math as m gives the module a shorter local name; from math import sqrt binds the function directly. Both reach the same module underneath, and neither downloads anything: the standard library is already part of the Python runtime.
An import runs once
An import runs the module’s top-level code once, the first time it is imported, and remembers the result. A second import in the same program reuses it rather than running it again.
That is why library modules keep definitions at the top level and work inside functions. It also means a module that prints a greeting will print it once when imported — surprising the first time, obvious afterwards.
Three import mistakes: calling sqrt(144) after import math raises NameError, because the name is math.sqrt; from math import * hides where names came from and can overwrite yours; and naming your own file random.py shadows the standard module in a real project, so the import finds your file instead.
Try these next
- Import
mathand printmath.pirounded to three decimals with an f-string. - Import
sqrtdirectly and call it without the module prefix. - Give
datetimethe aliasdt, then printdt.date(2026, 10, 7).
Mean, median and a square root
Run it, then add a fourth number and predict which of the two averages moves more before you run it again. The square root should follow the largest value.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does import math do?
