PYTHON Tutorial
Modules and packages are essential tools for organizing and reusing code in Python. A module is a single file containing Python code, while a package is a collection of related modules.
import <module_name>
.module_name.function()
).import <module_name>
statement.math
module: import math
.py
(e.g., my_module.py
).import <module_name>
..py
.__init__.py
in the directory.Consider the following example where a custom module my_module.py
is created:
# my_module.py
def add_numbers(a, b):
return a + b
In another Python script, we can import and use this module:
import my_module
result = my_module.add_numbers(5, 10)
print(result) # Output: 15
Modules and packages are powerful tools for organizing and reusing code in Python. By following these practical steps, developers can effectively use, import, and create modules and packages to enhance their Python programs.