Understand modular programming with modules and packages in Python.
A module is simply a file containing Python definitions and statements. Modules help organize code into manageable sections and enable reuse across multiple programs.
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.
# greetings.py
def say_hello(name):
return f"Hello, {name}!"
# main.py
import greetings
print(greetings.say_hello("Zoya"))
# 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))