This section is scheduled for 25 minutes (see course_timetable.md: 11:25–11:50).
By the end of this section, participants should be able to:
- Explain what a Python module is (and recognise standard-library examples).
- Use
import moduleand call functions via the module namespace (e.g.math.sqrt). - Use aliases with
as(e.g.import math as m). - Use
from module import nameand rename imported names safely. - Explain why namespaces matter, and why
from module import *is best avoided. - Use
dir(...)andhelp(...)to explore what a module provides.
- 11:25–11:28 (3 min) — What modules are + why we use them
- 11:28–11:35 (7 min) — Import a whole module + aliasing (
math,pathlib,sys,time) - 11:35–11:40 (5 min) — Import specific names (
from math import sqrt) + aliasing functions - 11:40–11:44 (4 min) — Namespaces matter:
math.logvslogging.log - 11:44–11:47 (3 min) — Why to avoid
from math import * - 11:47–11:50 (3 min) — Getting help:
dir()+help()(and what to do in notebooks)
Open the start of section 6 in the notebook.
Say:
- “A module is code packaged up so we can reuse it: usually a single
.pyfile, sometimes a package (a folder of files).” - “Modules can contain functions, classes, variables, and sometimes runnable code.”
- “The standard library is full of modules we lean on:
math,time,pathlib,sys,logging, …”
Quick check-in question:
- “What’s one module you’ve already used in Python?”
Teaching note (if someone sees a delay):
- “In JupyterLite / browser notebooks, the first import can take a moment while things load.”
Run the import math example.
What to say:
- “
import mathloads the module and binds the namemathin our program.” - “We then access what’s inside it with dot notation:
math.sqrt(16).” - “This dot prefix is the namespace — it tells us where the function came from.”
Then run the cell that imports multiple modules:
- “Python allows
import pathlib, sys, timein one line.” - “In real projects, you’ll often see one import per line for readability, but the idea is the same.”
Then run the alias example:
- “
import math as mis just a rename in our file: we still importedmath, but we’re referring to it asm.” - “Common in data science:
import numpy as np,import pandas as pd.”
Prompt:
- “Why might aliases be helpful? (Shorter names, or avoiding clashes.)”
Run the from math import sqrt example.
Say:
- “This pulls a specific name into the current namespace.”
- “Notice we call
sqrt(49)directly — nomath.prefix.” - “This is convenient, but it increases the chance of name collisions (we might already have a
sqrtvariable or function).”
Then run the aliasing-function example:
- “
from math import sqrt as square_rootgives us clarity and avoids conflicts.”
Micro-exercise (30–60 seconds):
- “Import
sinandpifrommathand check thatsin(pi / 2)is close to 1.”
Go to the “Why Namespaces Matter” example and run it.
What to say:
- “
logis an overloaded word: it can mean a logarithm (math.log) or a log message (logging.log).” - “The module namespace makes the intent obvious, to Python and to human readers.”
Run the from math import * cell.
Say:
- “This dumps everything from the module into our current namespace.”
- “Downsides:
- You can’t easily tell where a name came from.
- You can silently overwrite your own variables/functions.
- Linters and readers have a harder time understanding the code.”
Key message:
- “Prefer
import mathorfrom math import sqrt(a small, explicit list).”
Run dir(math).
Say:
- “
dir(module)lists the names available in that module.” - “It’s a quick way to discover what exists.”
Then run help(math).
Say:
- “
help(...)gives you documentation — it can be long, but it’s often enough to get moving.”
If you want a quicker demo in a notebook:
- “Try
help(math.sqrt)to focus on one function.”
Say:
- “Modules are how we reuse code safely and keep programs readable.”
- “Namespaces are your friend — they prevent confusion and collisions.”
- “Use
dir()/help()whenever you’re exploring unfamiliar code.”