Importing from the Standard Library
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A module is a Python file with ready-made tools inside. Python ships with hundreds of them, called the standard library. You load one with import and use it right away, with no download.
import: use a ready-made module
Module is a Python file that holds ready-made functions and values. You load it with import and reach its names with a dot, such as math.sqrt.
Writing the same square-root code in every program wastes time. Python already has it in the math module. Write import math, then reach each tool with a dot.
import mathprint(math.sqrt(144))print(math.pi)Example 1 · Rounding order values
import math
print(math.floor(9.99))print(math.ceil(9.01))print(math.sqrt(81))print(round(math.pi, 2))9 10 9.0 3.14
What this code does
floorrounds down andceilrounds up, so 9.99 and 9.01 end on 9 and 10.sqrtalways gives a float, so 81 becomes9.0.round(math.pi, 2)keeps two decimal places.
import math loads the module and creates one name, math. math.sqrt means the function sqrt that lives inside math. The dot keeps tools from different modules apart.
from and as: shorter names
Alias is a shorter name given to a module with as, such as import datetime as dt. Only the alias is created. The original name is not.
Typing math. many times gets long. Use from to pull out only the names you need, or as to give the module a short nickname.
from math import floor, ceilimport datetime as dtprint(floor(7.8))print(ceil(7.2))print(dt.date(2026, 11, 3).year)Example 2 · Short names with from and as
from math import floorimport datetime as dt
d = dt.date(2026, 3, 15)print(d.month)print(floor(-2.5))3 -3
What this code does
dt.date(...)builds a date.d.monthreads its month number.floor(-2.5)is -3, not -2, because floor always goes down to the smaller whole number.
After from math import floor, ceil, you write floor(7.8) with no math.. After import datetime as dt, the name is dt; the name datetime is not created.
random and datetime basics
Seed is the starting number for random. The same seed always gives the same sequence, which makes examples and tests repeatable.
Programs often need a random number or a date. The random module picks numbers, and datetime holds dates and time gaps.
import randomfrom datetime import date, timedelta
random.seed(7)print(random.randint(1, 6))due = date(2026, 11, 3) + timedelta(days=30)print(due)Example 3 · Repeatable random picks
import random
random.seed(10)marks = [72, 85, 90, 66, 78]print(random.choice(marks))random.shuffle(marks)print(marks)78 [90, 78, 85, 66, 72]
What this code does
seed(10)fixes the sequence, so these lines print the same result on every run.choicepicks one item.shufflereorders the list in place and returns nothing.
Example 4 · Days until a due date
from datetime import date
today = date(2026, 11, 3)due = date(2026, 12, 1)gap = due - todayprint(gap.days)print(due.strftime("%d %B %Y"))print(due.strftime("%A"))28 01 December 2026 Tuesday
What this code does
- Subtracting dates gives a gap.
gap.daysis the number of days. strftimeturns a date into text.%dis the day,%Bthe month name,%Athe weekday name.
randint(1, 6) includes both ends. seed(7) fixes the starting point, so every run prints the same number. Adding a timedelta of 30 days to a date gives a new date.
Next step
Check your understanding
9 quick questions