Module 19 · Debugging, Testing and Best Practices — topic 1 of 15
Debugging
A systematic approach to finding and fixing bugs: reproduce the problem, form hypotheses, gather evidence, isolate the cause, fix it, and confirm with tests — plus the tools Python offers for each step.
Learning objectives
- Follow a systematic debugging process
- Reproduce bugs with small, repeatable inputs
- Confirm fixes with checks that would have caught the bug
Definition
Debugging is the process of finding the cause of incorrect behaviour in a program and fixing it. Effective debugging is systematic: reproduce, hypothesise, test the hypothesis, fix, and verify.
Introduction
Every programmer spends a large part of their time debugging. The difference between a frustrating afternoon and a ten-minute fix is usually method, not luck.
Why it matters
Systematic debugging finds the real cause instead of patching symptoms, and leaves behind tests that stop the bug from returning.
Detailed explanation
Reproduce the bug reliably first. Note the exact input, steps and the expected versus actual output.
Shrink the input to the smallest case that still fails; small cases are far easier to reason about.
Form a hypothesis about the cause ('the loop skips the first item'), then gather evidence that confirms or rejects it — print values, use a debugger, or write a quick check.
Change one thing at a time so you know which change mattered.
Read error messages and tracebacks carefully; they often point directly at the problem (see Reading tracebacks).
Once fixed, add a test or check that reproduces the original bug, so it can't silently return (a regression test).
Explaining the code line by line to someone else — or to a rubber duck — often reveals the bug.
Take breaks: a fresh look frequently spots what hours of staring missed.
Syntax
- Reproduce first.
- Test one hypothesis at a time.
- Leave a check behind.
Code examples
An off-by-one bug
16.666666666666668 20.0
Narrowing down with small inputs
asha True ra False ravi_k False meera2026 True averyveryverylongname False
The third case shows the rule rejects underscores — maybe a bug, maybe intended. Small cases make such questions visible.
More examples
Checks that expose and confirm a fix
buggy failures: [(100, 10, -900, 90.0), (80, 50, -3920, 40.0)] fixed failures: []
Step-by-step explanation
- Each check records an input and the expected result, worked out by hand.
- The buggy version passes the 0% case but fails the others, pointing at how the percentage is used.
- After the fix, the same checks pass — and they stay as regression tests.
Expected output
Running “Checks that expose and confirm a fix” prints exactly:
Practical applications
- Fixing a bug reported by a user with a screenshot and exact steps.
- Investigating failing tests in a CI pipeline.
Best practices
- Reproduce before fixing.
- Shrink failing inputs.
- Turn every fixed bug into a test.
Common mistakes
Changing many things at once
Avoid
Prefer
You won't know which change fixed (or caused) the problem.
Important notes
- The following lessons cover tracebacks, print debugging, the VS Code debugger and testing tools.
Key takeaways
- Debugging is systematic: reproduce, hypothesise, test, fix, verify.
- Small failing inputs make causes clear.
- Regression checks keep bugs fixed.
Try it yourself
- 1Find and fix the bug in a function that should count vowels but misses capital letters.
- 2Write three checks for a temperature-conversion function and use them to find a deliberately introduced bug.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
10 questions cover the “Debugging, Testing and Best Practices” module. Answers and explanations are revealed after you submit.
- AdvancedWhy shouldn't assert be used to validate user input?
- AdvancedWhat is printed?
- AdvancedWhere should you usually start reading a Python traceback?