Modular Programming, Types of Modules & User-Defined Modules
When building real-world software, writing thousands of lines of code inside a single .py file makes the program difficult to read, test, and maintain. Modular Programming solves this problem.
1. What is Modular Programming & Why is it Important?
Modular Programming is a software design technique that divides a large, complex program into smaller, independent, and reusable files called Modules. Each module handles one specific functionality (e.g., auth.py for login, database.py for SQL queries, billing.py for invoices).
Math Functions
Database Logic
PDF / Output
Importance / Advantages of Modular Programming:
- Code Reusability: Write a module once and import it into multiple projects without rewriting code.
- Simplicity & Readability: Smaller files (100–200 lines) are much easier to understand than one giant 5,000-line file.
- Easy Debugging & Maintenance: If an error occurs in billing, you only need to check
billing.py. - Team Collaboration: Different developers can work on different modules simultaneously without conflict.
- Avoids Naming Collisions (Separate Namespaces): Two modules (
module1.pyandmodule2.py) can both have a function namedcalculate()without clashing.
2. What is a Module in Python?
A Module in Python is simply any file with a .py extension that contains executable Python code — such as functions, classes, and variables.
When you import a module, Python compiles it into bytecode (stored inside the __pycache__ folder as a .pyc file) so it loads faster next time.
3. Types of Modules in Python
Python modules are divided into two main categories:
| Feature | 1. Pre-Defined (Built-in / Standard) Modules | 2. User-Defined Modules |
|---|---|---|
| Definition | Modules that come pre-installed with Python’s Standard Library | Custom .py files created by the programmer for specific project needs |
| Creation | Written by Python core developers (in C or Python) | Written by the user/developer |
| Installation | Ready to use immediately with import |
Saved in the project directory and imported |
| Examples | math, os, sys, datetime, random, re, json |
my_math.py, student_utils.py, bank_account.py |
4. Creating and Importing a User-Defined Module
Let’s create our own module and use it inside another Python program!
Step 1: Create the Module File (mycalc.py)
Save the following code in a file named mycalc.py:
# File: mycalc.py (User-Defined Module)
pi_value = 3.14159
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def square(n):
return n * n
Step 2: Import and Use mycalc in main.py
In the same folder, create main.py. Python provides 4 different ways to import a module:
Way 1: Standard import module_name
Imports the whole module. You access members using the dot (.) operator:
import mycalc
print("Sum =", mycalc.add(10, 20)) # Output: 30
print("Pi =", mycalc.pi_value) # Output: 3.14159
Way 2: Importing with an Alias (import module as alias)
Gives a short nickname to the module name:
import mycalc as mc
print("Difference =", mc.subtract(50, 15)) # Output: 35
Way 3: Importing Specific Items (from module import item1, item2)
Imports only selected functions/variables directly, so you don’t need the module prefix:
from mycalc import add, square
print(add(5, 7)) # Output: 12 (No 'mycalc.' prefix needed)
print(square(9)) # Output: 81
Way 4: Importing Everything (from module import *)
Imports all functions and variables from the module (generally not recommended for large programs as it can cause name clashes):
from mycalc import *
print(add(100, 200))
5. Useful Built-in Module Inspection Tools
- The
dir()Function: Lists all functions, classes, and variables defined inside a module.import mycalc print(dir(mycalc)) # Shows ['add', 'pi_value', 'square', 'subtract', ...] - The
__name__ == "__main__"Idiom:- When a Python file is run directly, its built-in
__name__variable is set to"__main__". - When the file is imported as a module into another file,
__name__is set to the module’s filename ("mycalc"). - Putting test code inside
if __name__ == "__main__":ensures the test code runs only when the file is executed directly, and not when it is imported!
- When a Python file is run directly, its built-in