Debugging Basics
๐ What is Debugging? Debugging is the process of finding and fixing mistakes (bugs) in your code. Every programmer debugs every day. The word 'bug' comes from 1947 when an actual moth was found inside a computer causing it to fail! Today bugs are logic errors, typos, or wrong assumptions in your cโฆ

Appy Saysโฆ
Every professional developer spends 30โ50% of their time debugging. It's not a sign you're bad at coding โ it means you're coding. The best developers aren't the ones who write perfect code first time; they're the ones who find bugs fast.
What is Debugging?
A bug is an error or unexpected behaviour in your code. Debugging is the process of finding it and fixing it. There are three types of errors:
- โขSyntax errors โ code that Python can't even read (missing colon, wrong indent). Python tells you before running.
- โขRuntime errors โ code that runs but crashes partway through (dividing by zero, index out of range). Python shows a traceback.
- โขLogic errors โ code that runs without crashing but gives the wrong answer. These are the hardest to find.
Think of it like QA testing a game
Before a game ships, testers play it repeatedly looking for anything that breaks โ falling through the floor, enemies stuck in walls, scores not adding up. Debugging code is the same job: run it, observe what it does, find where it diverges from what it should do, fix it, test again.
Debugging Strategy โ Step by Step
- โขRead the error message โ it tells you the file, line number, and type of error
- โขAdd print() statements to see the value of variables at different points
- โขIsolate the problem โ comment out sections until you find which line causes the issue
- โขCheck your assumptions โ is the variable the type you think? Print
type(x) - โขSearch the error โ copy the error message into Google/Stack Overflow, others have had it
- โขFix one thing at a time โ don't change everything at once or you won't know what worked
Common Python Errors
- โขNameError: using a variable before creating it โ
print(score)beforescore = 0 - โขTypeError: wrong type in an operation โ
"age: " + 15(string + int) - โขIndexError: list index out of range โ accessing
list[5]when list has 3 items - โขIndentationError: wrong indentation โ Python's indentation is syntax, not style
- โขZeroDivisionError: dividing by zero โ check
if divisor != 0first
Key Facts
- โขThe word 'bug' comes from a literal moth found in a 1940s computer that caused a fault
- โขPython's traceback shows the exact chain of calls that led to the error โ read it bottom to top
- โข
print()debugging is used by professionals daily โ simple but powerful - โขPython has a built-in debugger called
pdbโ advanced but useful for complex bugs - โขGood variable names prevent many logic errors โ
total_scoreis harder to misuse thants
Watch Out: Don't Guess โ Investigate
The worst debugging habit is randomly changing code hoping something fixes it. Every change should be a hypothesis: 'I think the problem is X, so I'll change Y and see if the result changes.' This is how professionals debug efficiently.
Remember
When you hit an error: (1) Read the message. (2) Find the line. (3) Print nearby variables. (4) Fix one thing. (5) Test. Repeat. Debugging is detective work โ and once you're good at it, it actually becomes quite satisfying.
What You Learned
- โขThree types: syntax errors (can't read), runtime errors (crashes), logic errors (wrong result)
- โขRead the traceback โ it shows the exact line and type of error
- โขUse
print()to inspect variable values at different points - โขCheck types with
type(x)when a variable behaves unexpectedly - โขFix one thing at a time and test after each change
- โขDebugging skill matters as much as writing code โ it gets faster with practice
Key Facts
- โThe word 'bug' comes from a literal moth found in a 1940s computer that caused a fault
- โPython's traceback shows the exact chain of calls that led to the error โ read it bottom to top
- โ
print()debugging is used by professionals daily โ simple but powerful - โPython has a built-in debugger called
pdbโ advanced but useful for complex bugs - โGood variable names prevent many logic errors โ
total_scoreis harder to misuse thants
Real-World Examples
Remember
When you hit an error: (1) Read the message. (2) Find the line. (3) Print nearby variables. (4) Fix one thing. (5) Test. Repeat. Debugging is detective work โ and once you're good at it, it actually becomes quite satisfying.
Quick Quiz
What is a bug?