Modules and Packages

Code ko reuse karne aur organize karne ka professional tareeka

1. What is a Module?

Module ek Python file hoti hai jisme functions, classes ya variables hote hain. Hum modules ko import karke reuse karte hain.

# mymodule.py
def greet(name):
    return f"Hello {name}, welcome to AIkiPadhai!"

# main.py
import mymodule

print(mymodule.greet("Rahul"))

Output:

Hello Rahul, welcome to AIkiPadhai!

2. Built-in Modules

Python ke sath bahut sare built-in modules aate hain, jaise math, datetime, os.

import math

print(math.sqrt(25))   # 5.0
print(math.pi)         # 3.14159...

3. Installing External Packages

Python me hum pip ka use karke external libraries install karte hain.

# Install requests package
pip install requests

# Use the package
import requests

response = requests.get("https://api.github.com")
print(response.status_code)

4. What is a Package?

Package ek collection hota hai modules ka, jo ek folder me organized hote hain aur jisme ek __init__.py file hoti hai.

mypackage/
│
├── __init__.py
├── module1.py
└── module2.py
# module1.py
def add(a, b):
    return a + b

# main.py
from mypackage import module1

print(module1.add(5, 3))

Output:

8

Summary