๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

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โ€ฆ

8 min 10 XP Lesson 9 of 21
Debugging Basics
๐ŸŒ

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) before score = 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 != 0 first
๐Ÿ’ก

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_score is harder to misuse than ts
โš ๏ธ

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_score is harder to misuse than ts

Real-World Examples

โ€ข <strong>NameError:</strong> using a variable before creating it โ€” <code>print(score)</code> before <code>score = 0</code> โ€ข <strong>TypeError:</strong> wrong type in an operation โ€” <code>"age: " + 15</code> (string + int) โ€ข <strong>IndexError:</strong> list index out of range โ€” accessing <code>list[5]</code> when list has 3 items โ€ข <strong>IndentationError:</strong> wrong indentation โ€” Python's indentation is syntax, not style โ€ข <strong>ZeroDivisionError:</strong> dividing by zero โ€” check <code>if divisor != 0</code> first

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

1 / 2

What is a bug?