teach modules and packages....
with
definition,syntax,and examples codes,,,for 16 mark
Modules and Packages in Python
1. Modules
Definition
A module is a single Python file (.py) containing reusable code such as functions, classes, and
variables. Modules help organize code into logical, manageable sections, making it easier to
maintain and reuse [1] [2] [3] .
Syntax
Creating a Module:
Simply write Python code in a file and save it with a .py extension.
# mymodule.py
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Importing a Module:
Use the import statement.
import mymodule
mymodule.greeting("Jonathan")
print(mymodule.person1["age"])
Importing Specific Items:
Use from ... import ....
from mymodule import greeting
greeting("Alice")
Examples
# File: mod.py
s = "Python is fun!"
a = [1, 2, 3]
def foo(arg):
print(f'arg = {arg}')
class Foo:
pass
Using the module:
import mod
print(mod.s) # Output: Python is fun!
print(mod.a) # Output: [1, 2, 3]
mod.foo("Hello") # Output: arg = Hello
x = mod.Foo()
print(type(x)) # Output: <class 'mod.Foo'>
2. Packages
Definition
A package is a directory containing multiple modules and a special __init__.py file. Packages
allow for hierarchical structuring of the module namespace using “dotted module names” (e.g.,
mypackage.module1), making it easier to organize and manage large codebases [4] [5] [6] [3] .
Syntax
Creating a Package:
mypackage/
__init__.py
module1.py
module2.py
__init__.py can be empty or execute package initialization code.
Importing from a Package:
import mypackage.module1
mypackage.module1.functionA()
Or, for direct access:
from mypackage.module1 import functionA
functionA()
Examples
Suppose the following directory structure:
math_operations/
__init__.py
basic/
__init__.py
add.py
subtract.py
advanced/
__init__.py
multiply.py
divide.py
Contents of add.py:
def add(x, y):
return x + y
Using the package in your script:
from math_operations.basic.add import add
result = add(5, 3)
print(result) # Output: 8
3. Key Points and Differences
Feature Module Package
Definition Single .py file Directory with __init__.py and modules
Organize code into reusable
Purpose Organize related modules into a hierarchy
components
Import import package.module or from package
import module
Syntax import module
Example import math import numpy.linalg
4. Summary Table
Concept Definition Syntax Example Code Example
Single Python file with
Module import mymodule mymodule.greeting("John")
functions/classes
Directory with from mypackage
Package mypackage.module1.functionA()
__init__.py and modules import module1
In conclusion:
Modules help break code into logical files for reuse and clarity.
Packages group related modules, supporting scalable project structures and namespace
management.
Both are foundational for organizing, sharing, and maintaining Python code efficiently [1] [2]
[4] [5] [6] [3] [7] .
1. https://realpython.com/python-modules-packages/
2. https://www.w3schools.com/python/python_modules.asp
3. https://dev.to/nurarif151/day-4-of-90daysofupskill-modules-and-packages-in-python-417g
4. https://www.geeksforgeeks.org/python/python-packages/
5. https://docs.python.org/3/tutorial/modules.html
6. https://www.geeksforgeeks.org/python-packages/
7. https://en.wikiversity.org/wiki/Python_Programming/Modules