Modules and Imports
A module is a file full of ready-made tools.
Like a toolbox drawer labeled math or random.
You import the drawer, then use the tools inside.
This keeps your own file short and clean.
import a whole module
Write import and the module name.
Then use module.tool to call something inside.
from import for one tool
You can pull out just the tools you need.
Then you call them by name without the module prefix.
import with a short nickname
Use as to give a shorter name.
Helpful when the module name is long.
random for chance
The random module picks chance values.
Great for games and simple experiments.
Your own module idea
Any .py file can be a module.
Put helpers in helpers.py. Import them in main.py.
Think of separate toy boxes for separate jobs.
math for number tools
random for chance
datetime for dates and times
Your own files for your own helpers
dir and help previews
You can ask Python what is inside a module.
dir lists names. help shows docs in many environments.
Tip: Import only what you need. Keep imports at the top of the file.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.