Skip to content

Latest commit

 

History

History
137 lines (82 loc) · 4.91 KB

File metadata and controls

137 lines (82 loc) · 4.91 KB

Speaker notes — 6. Introduction to modules (11:25–11:50)

This section is scheduled for 25 minutes (see course_timetable.md: 11:25–11:50).

Outcomes (say this upfront)

By the end of this section, participants should be able to:

  • Explain what a Python module is (and recognise standard-library examples).
  • Use import module and call functions via the module namespace (e.g. math.sqrt).
  • Use aliases with as (e.g. import math as m).
  • Use from module import name and rename imported names safely.
  • Explain why namespaces matter, and why from module import * is best avoided.
  • Use dir(...) and help(...) to explore what a module provides.

Timing plan (25 minutes)

  • 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.log vs logging.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)

Talk track (what to say)

1) What is a module? (11:25–11:28)

Open the start of section 6 in the notebook.

Say:

  • “A module is code packaged up so we can reuse it: usually a single .py file, 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.”

2) Importing a whole module + aliases (11:28–11:35)

Run the import math example.

What to say:

  • import math loads the module and binds the name math in 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, time in 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 m is just a rename in our file: we still imported math, but we’re referring to it as m.”
  • “Common in data science: import numpy as np, import pandas as pd.”

Prompt:

  • “Why might aliases be helpful? (Shorter names, or avoiding clashes.)”

3) Importing specific names (11:35–11:40)

Run the from math import sqrt example.

Say:

  • “This pulls a specific name into the current namespace.”
  • “Notice we call sqrt(49) directly — no math. prefix.”
  • “This is convenient, but it increases the chance of name collisions (we might already have a sqrt variable or function).”

Then run the aliasing-function example:

  • from math import sqrt as square_root gives us clarity and avoids conflicts.”

Micro-exercise (30–60 seconds):

  • “Import sin and pi from math and check that sin(pi / 2) is close to 1.”

4) Why namespaces matter (11:40–11:44)

Go to the “Why Namespaces Matter” example and run it.

What to say:

  • log is 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.”

5) Why to avoid import * (11:44–11:47)

Run the from math import * cell.

Say:

  • “This dumps everything from the module into our current namespace.”
  • “Downsides:
    1. You can’t easily tell where a name came from.
    2. You can silently overwrite your own variables/functions.
    3. Linters and readers have a harder time understanding the code.”

Key message:

  • “Prefer import math or from math import sqrt (a small, explicit list).”

6) Getting help (11:47–11:50)

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.”

Wrap-up (last 30 seconds)

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.”