Sunday, May 31, 2026

4.3 Concept of Module, Library and Packages in Python

 


🐍 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

  1. Reuses code in different programs.
  2. Reduces code duplication.
  3. Makes code easy to organize.
  4. 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

  1. Groups related modules together.
  2. Organizes large projects efficiently.
  3. Improves code management.
  4. 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

  1. Provides ready-made code.
  2. Saves development time.
  3. Increases productivity.
  4. 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

  1. Installs external libraries easily.
  2. Updates existing packages.
  3. Removes unwanted packages.
  4. 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