What is Modular Programming?
Modular programming ka matlab hai humare code ko alag-alag files (modules) me split karna aur import karna, taaki hum reuse kar saken. Ye programs ko readable, scalable aur easy to maintain banata hai.
Python Module
Module typically ek Python file hoti hai jisme functions aur classes hote hain. Example: Python ka math module.
Example: Create your own module
Step 1: Ek file banaye mymodule.py:
# mymodule.py
def greet(name):
return f"Hello {name}!"
def add(a, b):
return a + b
Step 2: Import aur use karna in another file main.py:
# main.py
import mymodule
print(mymodule.greet("Amit"))
print(mymodule.add(10, 20))
Output:
Hello Amit!
30
Using from-import
# main.py
from mymodule import greet, add
print(greet("Ravi"))
print(add(5, 7))
Benefits of Modular Programming
- Code reuse
- Readable and maintainable code
- Organized project structure
- Easy to scale for large projects
Next Step
Ab hum seekhenge Error Handling jisme hum Python me exceptions aur try/except blocks sikhenge.