Python Modules and Packages — Full Notes
Python Modules and Packages — Full Notes What is a Module in Python? A module is simply a Python file (.py) that contains functions, variables, or classes that you can reuse in other programs. ✅ Helps in organizing code ✅ Promotes reusability ✅ Keeps code manageable Example: math_operations.py (This is a module you can import) def add(x, y): return x + y main.py import math_operations print(math_operations.add(5, 3)) # Output: 8 What is a Package in Python? A package is a folder that contains multiple modules (Python files), and has a special file called __init__.py . ✅ Used to organize related modules ✅ __init__.py makes it a package (can be empty or have setup code) Structure of a Package: my_package/ │ ├── __init__.py ├── module1.py └── module2.py To use a function from module1.py : from my_package import module1 module1.function_name() Importing Modules in Python ✅ Different ways to import: import math ...