Modules and import
Python comes with a large collection of ready-made code called the standard library. It is split into modules, each about one subject: maths, random numbers, dates, files and many more. A module is brought into your program with import. Learning to use modules means you stop writing things that already exist.
Importing a module
import math
print(math.sqrt(144))
print(math.pi)Output
12.0
3.141592653589793After import math, everything inside the module is reached with the module name and a dot. By custom, imports go at the very top of the file.
Other ways to import
from math import sqrt, pi
print(sqrt(81), pi)
import datetime as dt
print(dt.date(2026, 1, 26).year)Output
9.0 3.141592653589793
2026from module import namebrings in just the names you list. You then use them without the prefix.import module as shortgives the module a shorter nickname.
Avoid from math import *, which brings in everything. It hides where each name came from and can quietly replace your own variables.
The math module
import math
print(math.sqrt(2))
print(math.floor(7.9), math.ceil(7.1))
print(math.gcd(48, 36))
print(math.factorial(5))
print(round(math.pi * 7 ** 2, 2))Output
1.4142135623730951
7 8
12
120
153.94| Name | What it gives |
|---|---|
math.sqrt(x) | square root |
math.floor(x) | the whole number just below |
math.ceil(x) | the whole number just above |
math.gcd(a, b) | highest common factor |
math.factorial(n) | n factorial |
math.pi | the value of pi |
ceil is handy for questions such as "how many buses for 130 children if a bus holds 40?".
import math
print(math.ceil(130 / 40), "buses")Output
4 busesThe random module
random makes programs unpredictable: games, quizzes, picking a winner. The output below will differ every time you run it, so these examples first call random.seed(1). A seed makes the "random" choices repeat, which is useful for testing. Leave it out in real programs.
import random
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["Asha", "Ravi", "Meena"]))
cards = [1, 2, 3, 4, 5]
random.shuffle(cards)
print(cards)
print(random.sample(range(1, 50), 3))Output
2
Meena
[4, 2, 5, 3, 1]
[49, 29, 31]| Name | What it does |
|---|---|
random.randint(a, b) | a whole number from a to b, both included |
random.choice(list) | one item picked from a list |
random.shuffle(list) | mixes the list in place |
random.sample(items, k) | k different items |
random.random() | a float from 0 up to, but not including, 1 |
Note that randint includes both ends, unlike range. One warning: this module is for games and simulations. For passwords and anything to do with security, Python has a separate module called secrets.
The datetime module
Dates are harder than they look, with months of different lengths and leap years. Let the module do the work.
import datetime
republic = datetime.date(2027, 1, 26)
today = datetime.date(2026, 11, 12)
print(republic - today)
print((republic - today).days, "days to go")
print(republic.strftime("%d %B %Y"))
print(republic.strftime("%A"))Output
75 days, 0:00:00
75 days to go
26 January 2027
TuesdaySubtracting two dates gives the gap between them. strftime turns a date into text using format codes.
| Code | Meaning | Example |
|---|---|---|
%d | day of the month | 26 |
%m | month as a number | 01 |
%B | month name | January |
%Y | year | 2027 |
%A | day of the week | Tuesday |
The example above uses a fixed date so that its output stays the same. For the real current date, call datetime.date.today().
To move forwards or backwards in time, add a timedelta.
import datetime
start = datetime.date(2026, 11, 12)
due = start + datetime.timedelta(days=45)
print(due)
print(due.strftime("%d/%m/%Y"))Output
2026-12-27
27/12/2026Finding out what a module offers
Two built-in functions help you explore. dir(module) lists the names inside it. help(module.name) prints the explanation written by its authors. The complete reference is the official documentation at docs.python.org.
import math
names = [n for n in dir(math) if not n.startswith("_")]
print(len(names), "names, such as", names[:5])Output
62 names, such as ['acos', 'acosh', 'asin', 'asinh', 'atan']Modules beyond the standard library
Thousands of extra packages exist for web work, data and more. They are installed with a tool called pip. As a beginner you do not need any of them. Everything in these lessons uses only what comes with Python.
Do not name your file after a module
If you save your own program as random.py or math.py, then import random loads your file instead of the real module, and nothing works. Choose a different file name. This mistake produces a confusing AttributeError.
Try it yourself
Exercise 1. Use math to find the length of the long side of a right-angled triangle whose short sides are 5 and 12.
Show one answer
import math
print(math.sqrt(5 ** 2 + 12 ** 2))Output
13.0Exercise 2. Simulate rolling two dice and print both numbers and their total. Use random.seed(7) so the answer can be checked.
Show one answer
import random
random.seed(7)
a = random.randint(1, 6)
b = random.randint(1, 6)
print(a, b, a + b)Output
3 2 5Exercise 3. How many days are there between 15 August 1947 and 26 January 1950?
Show one answer
import datetime
gap = datetime.date(1950, 1, 26) - datetime.date(1947, 8, 15)
print(gap.days)Output
895Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.