Category: python
Python Modules
Published on 24 Jul 2026
Explanation
A module is a Python file (.py) that contains functions, variables, classes, and executable code. Modules help organize large programs
Code:
# math_module.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
# main.py
import math_module
print(math_module.add(10, 20))
print(math_module.subtract(50, 15))
Explanation
Python comes with a rich standard library containing hundreds of built-in modules. These modules provide ready-to-use functionality for mathematical calculations,
Code:
import math
import random
import datetime
print('Square Root:', math.sqrt(64))
print('Power:', math.pow(5, 2))
print('Random Number:', random.randint(1, 100))
current_date = datetime.datetime.now()
print('Current Date & Time:', current_date)
Explanation
A package is a collection of related Python modules organized inside a directory. Packages help structure large applications by grouping
Code:
# Project Structure
# mypackage/
# __init__.py
# calculator.py
#
# calculator.py
def multiply(a, b):
return a * b
# main.py
from mypackage.calculator import multiply
print(multiply(12, 5))
Explanation
pip is Python's package manager used to install, upgrade, and uninstall third-party libraries from the Python Package Index (PyPI). Developers
Code:
# Install a package pip install requests # Upgrade a package pip install --upgrade requests # List installed packages pip list # Generate requirements file pip freeze > requirements.txt # Install packages from requirements.txt pip install -r requirements.txt
Explanation
A virtual environment is an isolated Python environment that allows each project to have its own Python interpreter and installed
Code:
# Create a virtual environment python -m venv venv # Activate on Windows venv\Scripts\activate # Activate on macOS/Linux source venv/bin/activate # Install packages pip install requests # View installed packages pip list # Deactivate virtual environment deactivate