📦 Python Modules and Packages

Understand modular programming with modules and packages in Python.

📘 What is a Module?

Module in Detail Module vs Package Built in vs external

A module is simply a file containing Python definitions and statements. Modules help organize code into manageable sections and enable reuse across multiple programs.

📁 What is a Package?

A package is a way of organizing related modules into directories. A package must contain a special __init__.py file to be recognized as a package in Python.

🛠️ Creating and Using a Module

# greetings.py

def say_hello(name):
    return f"Hello, {name}!"

# main.py
import greetings
print(greetings.say_hello("Zoya"))

📦 Example of a Package

# Directory Structure:
# mypackage/
# ├── __init__.py
# └── tools.py

# tools.py

def multiply(a, b):
    return a * b

# main.py
from mypackage import tools
print(tools.multiply(4, 5))

🎯 Why Use Modules and Packages?

🔗 References