Lesson 01
Modules in Python — Visual & Easy to Understand
Understand why modules exist, how import works, and the difference between built-in and custom modules — with a toolbox analogy.
Real-life analogy: A module is like a toolbox. Instead of rebuilding tools every time, you keep them in one box and use them whenever you need them.
Use the playground and quiz at the bottom to turn these visuals into runnable code.
What you will learn
- Why modules exist — write once, use everywhere
- Function vs module — one file vs shared across projects
- What lives inside a module (variables, functions, classes)
- Built-in vs custom modules and how
importworks - Your path through Module 14: custom modules → import → reload → multi-file apps
Write once. Use everywhere.
Instead of copying the same functions into every program, save them in one .py file and import them whenever you need them.
# Without a module — copy logic into every file (bad)
c1 = 10 + 20
c2 = 30 + 40
# With a module — write once in arithops.py, import everywhere (good)
import arithops
arithops.sumop(10, 20)Why do modules exist?
Imagine you're building three different apps. All of them need the same calculator.
| Without modules | With modules |
|---|---|
| Copy the calculator into every project | Everyone shares one toolbox |
| Fix a bug in 3 places | Fix it once in the module |
| Code drifts out of sync | One source of truth |
Key idea: One module can be imported by many programs.
Definition: A module is a collection of global variable names, function names, and class names stored in a
.pyfile.
Function vs module
Students often confuse these two.
| Function | Module | |
|---|---|---|
| Scope | One file | Many files / programs |
| Analogy | One chef, one pot | Recipe book in a shared kitchen |
| Code | def sumop(a, b): ... |
# arithops.py then import arithops |
Function — reuse inside one file:
def sumop(a, b):
return a + b
print(sumop(10, 20))Module — reuse across many files:
# arithops.py
def sumop(a, b):
return a + b# main.py
import arithops
print(arithops.sumop(10, 20))What is inside a module?
A module is simply a Python file (.py) containing reusable code.
Example
# mathconstants.py
PI = 3.14
E = 2.71
def sumop(a, b):
return a + bTwo types of modules
| Type | Who writes it | Examples |
|---|---|---|
| Built-in | Python developers | math, sys, csv, calendar, re, functools |
| Custom | You or your team | arithops.py, bankinfo.py, mathconstants.py |
Python also auto-imports builtins — so print(), len(), and input() work without an explicit import.
How import works
Imagine this project folder:
main.py:
import arithops
arithops.sumop(10, 20)Python looks for arithops.py in the same project folder and loads it.
Built-in module example
math is a toolbox already installed with Python.
import math
print(math.pi)
print(math.sqrt(16))Custom module example
You create your own reusable toolbox.
| File | Code |
|---|---|
| arithops.py | def sumop(a, b): return a + b |
| main.py | import arithops → print(arithops.sumop(10, 20)) |
| Output | 30 |
Pick a toolbox — math, csv, or custom
Think of Python modules as different toolboxes. Try each in the playground:
| Toolbox | Import | Example call | Result |
|---|---|---|---|
| Math | import math |
math.sqrt(25) |
5.0 |
| CSV | import csv |
csv.__name__ |
'csv' |
| Custom | (inline def sumop) |
sumop(10, 20) |
30 |
import math
print(math.sqrt(25)) # 5.0
import csv
print(csv.__name__) # csvLearning roadmap
You're currently here:
- ✅ Lesson 1: Modules overview (this page)
- ⏭️ Next: Creating your own modules
| # | Lesson | Focus | Time |
|---|---|---|---|
| 2 | Custom Modules | Build .py files, __pycache__ |
35 min |
| 3 | import Statement | 4 import syntaxes | 40 min |
| 4 | from … import | 3 from-import syntaxes | 35 min |
| 5 | Module Reload | importlib.reload() |
25 min |
| 6 | Multi-Module Project | Menu + operations split | 45 min |
| 7 | Packages Overview | Function vs module vs package | 35 min |
| 8 | Creating Packages | Folder + __init__.py + modules |
40 min |
| 9 | Reusing Packages | sys.path.append + PYTHONPATH |
35 min |
| 10 | PYTHONPATH Global Setup | Windows · macOS · Linux | 40 min |
Interview memory card
One-line definition: A module is a Python
.pyfile containing reusable variables, functions, and classes that can be imported into other programs.
Key takeaways
- Toolbox analogy — keep tools once, use them in many projects
- Functions → one file; modules → shared across programs
- Built-in (
math,csv) vs custom (arithops.py) import arithops→ Python loadsarithops.pyfrom the same folder- Next: build your own module in Custom Modules
Loading playground…
Check your understanding
Modules overview quiz
What is the main purpose of Python modules?
1 / 10