Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

argument
A value you put inside the parentheses when you call a function, which the function then receives.
block
A group of lines that belong together under one opening line, such as the body of an if or a loop.
branch
A path a program can take when a condition sends it one way or another.
call site
The place in the code where a function is called, as opposed to where it is defined.
class
A template that bundles data with the code that works on it; each copy has its own values.
dictionary
A collection of key and value pairs, where a key is used to look up its value.
exception
An error that stops the normal flow of a program unless some code catches it.
function
A named piece of code that does one job and can be called whenever you need it.
function object
A function treated as a value, so it can be stored in a variable, passed to another function, or returned.
indentation
The spaces at the start of a line that show which block it belongs to.
integer
A whole number with no decimal point, such as 3, 0 or -12.
list
An ordered collection of values that you can grow, shrink and reach into by position.
parameter
A name in a function definition that stands for the value the caller will supply.
return value
The result a function hands back to the code that called it.
scope
The part of a program where a name can be used; a name made inside a function is not visible outside it.
traceback
The report Python prints when a program fails, listing the calls that led to the error.
variable
A name that refers to a value, so you can use that value again later or change it.

Python from the ground up

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.

Part 10 of 15

Advanced15 min readBack to Courses

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.

Output
Click "Run" to execute your code

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 math and print math.pi rounded to three decimals with an f-string.
  • Import sqrt directly and call it without the module prefix.
  • Give datetime the alias dt, then print dt.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.

Output
Click "Run" to execute your code

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?