Lesson 02
Building Custom Python Modules — Visual & Relatable
Learn how to create your own .py files, organize reusable code, and understand what __pycache__ really does.
Real-life analogy: A custom module is like creating your own toolbox. You build it once, give it a name, and everyone on your team can use it.
Use the playground and quiz at the bottom to practice building module-style code.
What you will learn
- Why teams build custom modules instead of copy-pasting code
- The 3 steps to build a module (variables → functions → classes)
- Three real patterns: constants, arithmetic, and banking
- What
__pycache__and.pycfiles really are - Rules for clean, reusable module design
Build once. Import forever.
A custom module is simply a Python file containing variables, functions, and classes that other programs can reuse.
Why build a custom module?
Imagine you're a carpenter.
Instead of carrying loose tools every day, you organize them into one toolbox.
| Without module | With module |
|---|---|
| Tools scattered everywhere | Everything in one toolbox |
| Hard to reuse | Reusable anywhere |
| Same fix in many places | Fix once in the module |
One module can serve many applications.
The 3 steps to build a module
Every custom module follows the same pattern.
| Step | Purpose |
|---|---|
| 1 | Store constants or configuration |
| 2 | Write reusable functions |
| 3 | Create classes when needed |
Save as FileName.py. The filename (without .py) becomes the module name.
Example 1 — Constants module
Think of this as your settings file.
# mathconstants.py
PI = 3.14
E = 2.71Now every program can reuse PI instead of rewriting it.
Example 2 — Arithmetic module
This becomes your calculator toolbox.
# arithops.py
def sumop(a, b):
return a + b
def subop(a, b):
return a - b
def mulop(a, b):
return a * bLater you'll call them like:
import arithops
print(arithops.sumop(10, 20)) # 30Example 3 — Banking module
Imagine your bank has one official interest calculator.
# bankinfo.py
BNAME = "BOI"
ADDR = "HYD"
def simple_interest(p, t, r):
si = (p * t * r) / 100
return siAnother program can import this instead of rewriting the formula.
What happens after you save?
When you save arithops.py, Python quietly creates another file for speed.
Think of it like this
| File | Analogy |
|---|---|
.py |
Your editable recipe |
.pyc |
Python's pre-cooked version |
__pycache__ |
Storage shelf for compiled recipes |
__pycache__/
arithops.cpython-312.pyc
mathconstants.cpython-312.pyc
bankinfo.cpython-312.pycProject folder structure
This is what a beginner project looks like.
All these .py files are modules.
Build your own module — try in the playground
Pick a module type and run it in the playground below:
| Type | File | What it stores |
|---|---|---|
| Constants | mathconstants.py |
PI = 3.14, E = 2.71 |
| Arithmetic | arithops.py |
sumop, subop, mulop |
| Bank | bankinfo.py |
BNAME, ADDR, simple_interest(p, t, r) |
# Constants — reusable values
PI = 3.14
E = 2.71
print(PI, E)Rules for custom modules
- One module = one
.pyfile - Keep reusable code at the top level (not nested inside another function)
- Place importer and module in the same folder (beginner level)
- Pass values as parameters instead of using
input()
Interview memory card
Remember: You create the
.pyfile — Python creates the.pycfile.
Next lesson
You'll learn the 4 ways to import modules in import Statement:
import moduleimport module as aliasfrom module import namefrom module import *
Loading playground…
Check your understanding
Custom modules quiz
What are the three steps to develop a programmer-defined module?
1 / 10