Big programs are not written in one file. They are organized into modules and packages.
This is where Python starts feeling like real software engineering.
A module is simply a Python file (.py) containing code.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
import math_module
print(math_module.add(5, 3))
print(math_module.subtract(10, 4))
import math_module as mm
print(mm.add(2, 3))
from math_module import add
print(add(7, 5))
Python comes with many built-in modules.
import math
import datetime
print(math.sqrt(16))
print(datetime.datetime.now())
A package is a folder that contains multiple modules.
project/
│
├── utils/
│ ├── __init__.py
│ ├── math_utils.py
│ └── string_utils.py
│
└── main.py
def multiply(a, b):
return a * b
from utils.math_utils import multiply
print(multiply(4, 5))
__init__.py tells Python that the folder is a package.
from .math_utils import multiply
Beginners write scripts. Engineers design systems.
Modules and packages are the foundation of real Python projects.
If your project is more than one file, you need modules. If it grows further, you need packages.
Next part: File Handling (Read / Write / Manage Files).