Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate35 min
Building custom Python modules — Lesson 2 toolbox hero

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 .pyc files 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
Scattered tools vs organized toolbox carpenter analogy
App A and App B share one arithops.py custom module toolbox

One module can serve many applications.


The 3 steps to build a module

Every custom module follows the same pattern.

Three steps variables functions classes to build a module
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 settings file stores PI and E constants
python
# mathconstants.py
PI = 3.14
E = 2.71

Now every program can reuse PI instead of rewriting it.


Example 2 — Arithmetic module

This becomes your calculator toolbox.

arithops.py calculator toolbox sum sub mul operations
python
# arithops.py
def sumop(a, b):
    return a + b

def subop(a, b):
    return a - b

def mulop(a, b):
    return a * b

Later you'll call them like:

python
import arithops

print(arithops.sumop(10, 20))   # 30
import arithops call sumop output 30

Example 3 — Banking module

Imagine your bank has one official interest calculator.

bankinfo.py bank module BNAME ADDR simple_interest formula
python
# bankinfo.py
BNAME = "BOI"
ADDR = "HYD"

def simple_interest(p, t, r):
    si = (p * t * r) / 100
    return si

Another program can import this instead of rewriting the formula.

Simple interest calculation Principal Time Rate output

What happens after you save?

When you save arithops.py, Python quietly creates another file for speed.

arithops.py compiles to pycache arithops.cpython-312.pyc bytecode

Think of it like this

File Analogy
.py Your editable recipe
.pyc Python's pre-cooked version
__pycache__ Storage shelf for compiled recipes
text
__pycache__/
    arithops.cpython-312.pyc
    mathconstants.cpython-312.pyc
    bankinfo.cpython-312.pyc

Project folder structure

This is what a beginner project looks like.

project folder main arithops bankinfo mathconstants pycache

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)
python
# Constants — reusable values
PI = 3.14
E = 2.71
print(PI, E)

Rules for custom modules

  1. One module = one .py file
  2. Keep reusable code at the top level (not nested inside another function)
  3. Place importer and module in the same folder (beginner level)
  4. Pass values as parameters instead of using input()

Interview memory card

Interview memory card py file pycache pyc edit py only

Remember: You create the .py file — Python creates the .pyc file.


Next lesson

You'll learn the 4 ways to import modules in import Statement:

  • import module
  • import module as alias
  • from module import name
  • from module import *

Loading playground…

Check your understanding

Custom modules quiz

What are the three steps to develop a programmer-defined module?

1 / 10