🟡 Advanced Python · Lesson 30
Python Code Testing और Debugging
Testing और Debugging क्या है?
Testing और Debugging ka matlab hai: Testing checks whether code works correctly. Debugging helps find and fix errors. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki checking code automatically jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye checking code automatically me kaam aata hai.
- Ye finding wrong output se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| assert | Statement used to verify that a condition is true. |
| unit test | Small test that checks one part of code. |
| bug | A mistake in code that causes wrong behavior. |
| traceback | traceback is an important term in this topic. |
| print debugging | print debugging is an important term in this topic. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
Basic Pattern
def add(a, b):
return a + b
assert add(2, 3) == 5
assert add(-1, 1) == 0
print("All tests passed")Complete Example Program
Python – testing-debugging.py
def add(a, b):
return a + b
assert add(2, 3) == 5
assert add(-1, 1) == 0
print("All tests passed")Expected Output
All tests passed
Program Explanation
def add(a, b):creates a reusable function.return a + bsends a result back from the function.assert add(2, 3) == 5performs the next step of the program logic.assert add(-1, 1) == 0performs the next step of the program logic.print("All tests passed")displays information or calculated result on the screen.
Practical Uses
- Checking code automatically.
- Finding wrong output.
- Improving reliability.
Common Mistakes
- Making code complex when a simple function or class is enough.
- Not handling possible errors or edge cases.
- Mixing project dependencies instead of using a virtual environment.
Practice Tasks
- Program ko
testing-debugging.pyfile me type karke run karein. - Values change karke output compare karein.
- checking code automatically par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
Testing and Debugging ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.