- Learning Objectives
- Prerequisites
- Introduction
- What is a Module?
- Why are Modules Required?
- Advantages of Modules
- Types of Modules
- Built-in Modules
- User-Defined Modules
- Module Execution
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand what a module is.
- Learn why modules are used.
- Differentiate built-in and user-defined modules.
- Understand how modules improve code organization.
- Learn how Python executes modules.
Before learning this chapter, you should know:
- Functions
- Classes
- File Handling Basics
- Python Program Structure
As Python programs grow larger,
writing all the code in a single file becomes difficult to manage.
To improve readability, maintainability, and code reuse,
Python allows programs to be divided into smaller files called modules.
A module groups related functions, classes, and variables into a single file, making programs easier to organize and maintain.
A module is a Python file (.py) that contains reusable code such as functions, classes, variables, and executable statements.
A module is a Python file that contains reusable code.
Without modules:
- Programs become large and difficult to maintain.
- Code duplication increases.
- Reusability decreases.
- Collaboration becomes harder.
Using modules:
- Code is organized.
- Reusability improves.
- Maintenance becomes easier.
- Team development becomes simpler.
Consider an online shopping application.
Instead of writing everything in one file, the application can be divided into modules:
login.pypayment.pyorders.pyproducts.pynotifications.py
Each module handles a specific responsibility.
- Improves code organization.
- Encourages code reuse.
- Simplifies maintenance.
- Supports team development.
- Makes debugging easier.
- Reduces code duplication.
Python supports two main types of modules:
Modules provided by Python.
Examples:
mathrandomdatetimeossys
Modules created by the programmer.
Example:
calculator.py
Python provides many ready-to-use modules.
Examples include:
| Module | Purpose |
|---|---|
math |
Mathematical operations |
random |
Random number generation |
datetime |
Date and time handling |
os |
Operating system interaction |
sys |
Access to Python runtime information |
A user-defined module is simply a .py file created by the programmer.
It can contain:
- Variables
- Functions
- Classes
- Executable statements
These modules can be imported and reused in other Python programs.
When a module is imported:
- Python locates the module.
- The module is loaded into memory.
- The module code is executed once.
- The module object becomes available for use.
Python Program
↓
import Module
↓
Python Searches Module
↓
Module Loaded
↓
Module Executed
↓
Functions and Classes Available
↓
Program Continues
- Code Reusability
- Better Organization
- Easier Maintenance
- Improved Readability
- Modular Design
- Give modules meaningful names.
- Keep each module focused on a single responsibility.
- Avoid writing unnecessary executable code at the module level.
- Group related functions and classes together.
- Reuse existing modules instead of duplicating code.
Creating very large modules containing unrelated functionality.
Using unclear module names.
Duplicating code instead of importing reusable modules.
A module is a Python file containing reusable code such as functions, classes, variables, and executable statements.
Modules improve code organization, reusability, readability, and maintainability.
- Built-in modules
- User-defined modules
mathrandomdatetimeossys
A Python module created by the programmer for reusable application-specific functionality.
- Module → Reusable Python file.
- Built-in Module → Provided by Python.
- User-Defined Module → Created by the programmer.
- Modules improve organization and code reuse.
In this chapter, we learned:
- Modules
- Types of Modules
- Built-in Modules
- User-Defined Modules
- Module Execution
- Advantages
- Best Practices
- Interview Questions
- Quick Revision
Write a Python program to import the math module and calculate the square root of a number.
import math
number = 64
result = math.sqrt(number)
print("Square Root :", result)Square Root : 8.0
The built-in math module provides mathematical functions.
The sqrt() function calculates the square root of a number.
import random
number = random.randint(1, 100)
print("Random Number :", number)Random Number : 57
The random module generates random values.
randint() returns a random integer within the specified range.
import datetime
today = datetime.datetime.now()
print(today)2026-07-01 10:30:15.245678
The datetime module is used to work with dates and times.
now() returns the current date and time.
import os
print(os.getcwd())C:\PythonProjects
The os module provides operating system related functionality.
getcwd() returns the current working directory.
def add(a, b):
return a + bimport calculator
result = calculator.add(10, 20)
print(result)30
The calculator.py file acts as a user-defined module.
It can be imported and reused in other programs.
def add(a, b):
return a + b
def sub(a, b):
return a - bimport calculator
print(calculator.add(15, 5))
print(calculator.sub(15, 5))20
10
A module can contain multiple related functions.
company_name = "OpenAI"import company
print(company.company_name)OpenAI
Modules can also store reusable variables.
class Student:
def display(self):
print("Student Class")import student
obj = student.Student()
obj.display()Student Class
Modules can contain classes in addition to functions and variables.
import math
import random
print(math.factorial(5))
print(random.randint(1, 10))120
7
A single program can import multiple modules.
Each module provides different functionality.
def multiply(a, b):
return a * bimport calculator
print(calculator.multiply(5, 6))import calculator
print(calculator.multiply(10, 20))30
200
One module can be reused across multiple Python programs.
This is one of the biggest advantages of modular programming.
Program Starts
↓
import Module
↓
Python Searches Module
↓
Module Loaded
↓
Module Executed
↓
Required Function Called
↓
Result Returned
↓
Program Ends
Program
│
├── Main Module
│
└── Imported Module
│
├── Variables
├── Functions
└── Classes
↓
Program Ends
- Improves code reusability.
- Simplifies maintenance.
- Organizes related code.
- Reduces duplication.
- Makes large applications easier to manage.
- Keep modules focused on a single responsibility.
- Use meaningful module names.
- Group related functions and classes together.
- Import only the modules you need.
- Reuse existing modules whenever possible.
Creating modules with unrelated functionality.
Giving modules confusing names.
Duplicating code instead of importing reusable modules.
Forgetting to keep user-defined modules in the correct project directory.
A module is a Python file containing reusable functions, classes, variables, and executable statements.
- Built-in modules
- User-defined modules
Modules improve code organization, reusability, readability, and maintainability.
Yes.
A module can contain functions, classes, variables, and executable code.
Yes.
One module can be imported and reused by multiple Python programs.
mathrandomdatetimeossys
Python locates the module, loads it into memory, executes it once, and makes its contents available to the importing program.
- Import the
mathmodule and calculate a square root. - Generate random numbers using the
randommodule. - Create a user-defined module with arithmetic functions.
- Create a module containing a class.
- Import multiple modules into the same program.
- Module → Reusable Python file.
- Built-in Module → Provided by Python.
- User-Defined Module → Created by the programmer.
- A module can contain functions, classes, variables, and executable statements.
- Modules improve code organization and reusability.
In this chapter, we learned:
- Introduction to Modules
- Built-in Modules
- User-Defined Modules
- Creating Modules
- Importing Modules
- Module Reusability
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision