Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 07 / Topic 3 of 9

Writing your own module

Save functions in a file, for example shop_utils.py. Then any file in the same folder can import it by name (without .py).

Real-world example: a kirana shop keeps its GST and discount helpers in one file.

# shop_utils.py
GST_RATE = 0.18

def add_gst(price):
    return round(price * (1 + GST_RATE), 2)

def format_rupees(amount):
    return f"₹{amount:,.2f}"
# bill.py (same folder)
import shop_utils

total = shop_utils.add_gst(1000)
print(shop_utils.format_rupees(total))   # ₹1,180.00

See examples/shop_utils.py and examples/02_use_my_module.py.

Do not name your file the same as a standard module (random.py, math.py, json.py). Your file will be imported instead of the real one.

🔍 Going deeper: after the first import you will see a __pycache__/ folder with .pyc files. These are saved bytecode (the compiled form of your module) so the next import is faster. They are safe to delete and should go in .gitignore.

Done here? The practice set for Part 7 — 7 exercises with solutions — is on the part page.

Search the guide