Keep related functions together in a file you can import, like a toolbox.
Every Python program you will ever use imports other people's modules. Now you will build your own.
You have written several functions. A module is a file that holds related functions, so any program can import them.
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Predict
modules.py
from turtle import *
from shapes import square, triangle
square(80)
triangle(80)
This program never defines square or triangle. What does it draw? Where do you think they come from?
RunRun it and compare the result with your prediction.
Run
Run it. The line from shapes import square, triangle brings two ready-made functions into your program.
modules.py
from turtle import *
from shapes import square, triangle
square(80)
triangle(80)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.
One bug, two copies
Two separate programs both need wall, so each has its own copy. Nothing here crashes; the problem is the copying. Pasting from My tools works the same way: each paste is a copy, and editing the tool does not change copies already pasted. This is the problem a module solves.
The walls should close into squares. Find every place the fix has to go.
You fix one copy. What about the other?
The walls do not close: the turn inside wall is wrong. Click every line you would have to change.
program_a.py
program_b.py
InvestigateTrace how each line of code connects to what you see.
Where does it come from?
Open Reference, find Module: shapes and click Show how it works. Compare it with the functions you have written.
modules.py
from turtle import *
from shapes import square, triangle
square(80)
triangle(80)
Where does square come from?
shapes.py
from turtle import *
defsquare(size):
for i inrange(4):
forward(size)
right(90)
main.py
from turtle import *
from shapes import square
defsquare(size):
circle(size / 2)
square(80)
Try it yourselfYour edits are saved in this browser.
ModifyChange the code on purpose and check the result.
Modify
These three functions are all in one file, and the wall does not close. Click + module, move them into it, and import house back into main.py. Then fix the turn in wall, once, in the module. Work it out from the shape, as you did with variables: 360 / 4, not a guessed number.
Try it yourselfYour edits are saved in this browser.
Need a hint?
Click + module, give it a name, then cut the three functions and paste them there.
Another hint
Your program needs a line at the top: from yourname import house
Another hint
The walls turn 80 degrees. Work the turn out from the shape instead: right(360 / 4).
InvestigateTrace how each line of code connects to what you see.
Mix it up
Work this out on paper first.
# shapes.pydefsquare(size):
for i inrange(4):
forward(size)
right(90)
# main programfrom shapes import *
square(30)
square(50)
How far does the turtle draw in total, counting only the lines (not the turns)?
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Make
Create a module of your own with at least three related functions. Import it and use it to draw a small scene.
Try it yourselfYour edits are saved in this browser.
Need a hint?
Pick a theme for your module, such as trees, stars or buildings.
Another hint
Keep drawing calls in main.py. The module should only contain functions.
ModifyChange the code on purpose and check the result.
Extend a street
A street is a few buildings repeated, and each building comes from a module. Run it. Add a roof colour, a taller house, or a gap in the street. You are changing the plan without rewriting any of the shapes.
Try it yourselfYour edits are saved in this browser.
For teachers
Students import functions from a module (a separate file of related functions), see why copied code costs more to fix, then build a module of their own. About 55 minutes.
I can import functions from a module and use them.
I can explain why one shared module is easier to fix than copies of the same code.
I can move related functions into a module of my own.
Before class
A module is a Python file of functions that other programs can use. from shapes import square means "find the file shapes.py and bring in its square function". In the playground, + module makes a new file tab. Before class, do the Modify step yourself: move the three house functions into a module and import house.
Running the lesson
Predict: the program draws a square and a triangle but never defines them. Ask where they might come from.
Investigate (one bug, two copies): both programs have the same wrong turn. Fixing it means editing twice, and missing one leaves a bug. That cost is the reason modules exist.
Point out that My tools works the same way as copying: each paste is a separate copy.
Investigate (where does it come from?): open the shapes module from the Reference panel. Ask: what happens if main.py defines its own square after importing one?
Modify: students move three functions into a module, import house, and fix the turn once.
Make: a theme helps (trees, stars, buildings). Keep the drawing calls in main.py; the module only holds functions.
The hard part (on purpose)
Seeing why a module beats copying, and that a def with the same name replaces an import. Ask how many places they'd have to fix if the code were copied.
When students get stuck
A ModuleNotFoundError or ImportError. Ask: Is the module's file name exactly the name after from, without .py? Fix: The tab name and the import must match exactly, in lowercase.
The drawing happens twice, or as soon as the module is imported. Ask: Are there calls (not just defs) inside the module? Fix: Keep only def blocks in the module. Calls go in main.py.
An imported function behaves differently from what the module says. Ask: Is there a def with the same name in main.py? Fix: A def in main.py with the same name replaces the imported one. Rename or delete one of them.
Stretch and support
Stretch: Write a module with a docstring for each function (a first line in triple quotes). The docstring becomes the hint on the function's card in the Reference panel.
Support: Do the Modify step together on the projector, then let students make a two-function module before attempting three.
Where it leads
Next unit makes programs react to clicks and keys. Every real Python program imports modules others wrote (random, math, and libraries like Gradio). Class ideas: each student builds a module for a theme and a classmate imports it; a class town built from everyone's modules.
What each step is for
one bug, two copies: Copied code multiplies the work of fixing it; one shared module is fixed once.
where does it come from?: An import gives a program names; a def with the same name replaces the imported one without warning.
Mix it up: A module is functions kept in a file. Calling them still follows the same rules.
Now you can
Import functions from a module and use them.
Explain why one shared module is easier to fix than copies of the same code.