I1 · Python Programming for AI

Modules and Libraries

A module is a Python file of functions and values that other programs can import. A library or package is a collection of modules. Python's standard library comes built in, with modules such as math, random and datetime; third-party packages such as NumPy are installed with pip.

10 min Intermediate Jump to the activity

What you need: The earlier topics of this module.

Definition

Why rewrite what someone has already written and tested?

A module is a Python file of functions and values that other programs can import. A library or package is a collection of modules. Python's standard library comes built in, with modules such as math, random and datetime; third-party packages such as NumPy are installed with pip.

In simple words

import math gives you a toolbox: math.sqrt(16) is 4.0 and math.pi is 3.14159…. import random lets you roll dice. You can import just one tool, from math import sqrt, or give a module a short name, import numpy as np. On your own computer, pip install numpy downloads a package.

Why it matters

Nobody builds AI from nothing. Libraries such as NumPy, pandas, scikit-learn and PyTorch hold years of expert work. Knowing how to import and read a library's documentation is a core skill.

How it works

ModuleUseful forExample
mathSquare roots, powers, pi, roundingmath.sqrt(81)
randomRandom choices and numbersrandom.choice(names)
datetimeDates and timesdate(2026, 1, 26)
statisticsMean, median, modestatistics.median(xs)
collectionsCounter, deque and moreCounter(words)

In code

Four standard modules in action. The seed makes the random rolls the same every time.

modules.py
import math
import random
from datetime import date
from statistics import mean, median

print("Square root of 81:", math.sqrt(81))
print("Pi to 3 places:", round(math.pi, 3))

random.seed(42)
rolls = [random.randint(1, 6) for _ in range(5)]
print("Five dice rolls:", rolls)

day = date(2026, 1, 26)
print(day, "is a", day.strftime("%A"))

scores = [72, 91, 64, 85, 78]
print("Mean:", mean(scores), "Median:", median(scores))

Output

Square root of 81: 9.0
Pi to 3 places: 3.142
Five dice rolls: [6, 1, 1, 6, 3]
2026-01-26 is a Monday
Mean: 78 Median: 78

Try it yourself

Which module would you import?

Match each task to the standard module that helps.

  • Find the square root of 50
  • Pick a random winner from a list
  • Work out how many days until a festival
  • Find the median of some marks
  • Count how often each word appears

Common mistakes

  • Naming your own file math.py or random.py, which hides the real module.
  • Forgetting the module name: sqrt(16) fails after import math; use math.sqrt(16).
  • Installing packages inside a program. Run pip in the terminal, once.

Check yourself

Question 1

1. After import math, how do you get the square root of 9?

Question 2

2. What does random.seed(42) do?

Question 3

3. How is a third-party package installed?

Summary

  • Modules are reusable Python files; packages group modules.
  • The standard library is built in; pip installs others.
  • import, from … import and import … as are the three forms.

Finished reading?

Sources: Python Standard Library