🐍 Modules, Packages & Libraries
in Python
📄 Module
Definition
A module is a single Python file containing reusable code such as functions, classes, variables, and statements that can be imported and used in other Python programs.
Uses /
Importance / Advantages
- Reuses code in different
programs.
- Reduces code duplication.
- Makes code easy to organize.
- Simplifies testing and
debugging and improves code maintenance.
Types
of Modules
- Built-in Modules: Provided by Python.
- User-Defined Modules: Created by programmers.
Module
Examples
Built-in
Modules
- math
- random
- datetime
- os
- sys
Example:
import
math
print(math.sqrt(25))
User-Defined
Module
calculator.py
def add(a,
b):
return a + b
Additional
Points
- File extension: .py
- Import methods:
import
module_name
from module_name import function
import module_name as alias
📦 Package
Definition
A package is a collection of related Python modules organized in a directory (folder) to provide a structured way of managing and reusing code.
Uses /
Importance / Advantages
- Groups related modules
together.
- Organizes large projects
efficiently.
- Improves code management.
- Prevents naming conflicts and
makes applications scalable.
Package
Structure
calculator/
├──
add.py
├──
subtract.py
└── __init__.py
Package
Examples
- numpy.random
- django.contrib
- email.mime
Import
Example
from calculator.add
import add
Additional
Points
- Contains one or more modules.
- Usually includes __init__.py.
- Can contain sub-packages.
- Helps in hierarchical
organization of code.
🏛️ Library
Definition
A library is a collection of packages and modules that provides reusable functionality for specific tasks, allowing developers to perform complex operations without writing code from scratch.
Uses / Importance / Advantages
- Provides ready-made code.
- Saves development time.
- Increases productivity.
- Reduces coding effort and helps
perform complex tasks easily
Types
of Libraries
1.
Standard Library
Comes
pre-installed with Python.
Examples:
- math
- os
- random
- datetime
2.
External Library
Installed
using pip.
Examples:
- NumPy
- Pandas
- Matplotlib
- Requests
- TensorFlow
Popular
Python Libraries
|
Library |
Use |
|
NumPy |
Numerical Computing |
|
Pandas |
Data Analysis |
|
Matplotlib |
Data Visualization |
|
Requests |
API Calls |
|
TensorFlow |
Machine Learning |
|
OpenCV |
Computer Vision |
|
Django |
Web Development |
Interview
Examples
- Module: math, random, calculator.py
- Package: numpy.random, django.contrib
- Library: NumPy, Pandas, Matplotlib,
TensorFlow
📦 Python pip
Definition
pip
(Pip Installs Packages)
is Python's package manager used to install, update, and remove external
packages and libraries.
Common Commands
pip
install numpy
pip uninstall numpy
pip list
pip --version
Importance
of pip
- Installs external libraries
easily.
- Updates existing packages.
- Removes unwanted packages.
- Manages project dependencies
and simplifies library management.
📊 Difference Between Module, Package
& Library
|
Feature |
Module |
Package |
Library |
|
Meaning |
Single Python file |
Folder of modules |
Collection of packages/modules |
|
Size |
Smallest unit |
Medium unit |
Largest unit |
|
Extension |
.py file |
Directory |
Collection |
|
Purpose |
Reuse code |
Organize modules |
Provide complete functionality |
|
Example |
math.py |
numpy.random |
NumPy |
🎤 Final Interview One-Liner
A
module is a single Python file, a package is a collection of related modules,
and a library is a collection of packages and modules that provides reusable
functionality.

No comments:
Post a Comment