Tutorials Home   >   Core Programming Concepts   >   Debugging Basics

Debugging Basics

Introduction to Debugging

Imagine trying to assemble a new gadget, but one piece doesn’t fit. You inspect it, figure out why, and fix it. In programming, debugging is the process of finding and fixing errors in your code.

What is Debugging?

Debugging is the process of:

  • Identifying bugs (errors) in a program.

  • Understanding why the code behaves incorrectly.

  • Correcting the code to make it work as expected.

Why Debugging is Important

  • Ensures correct program behavior.

  • Helps prevent crashes or unexpected results.

  • Improves code quality and reliability.

  • Strengthens your problem-solving skills.

Types of Errors in Programming:

Error Type Description Example
Syntax Error Mistake in the code grammar print("Hello" missing )
Runtime Error Error while program is running 10 / 0 → ZeroDivisionError
Logical Error Code runs but gives wrong results Using + instead of *

Debugging mostly deals with runtime and logical errors.


Page 2: Common Debugging Techniques

1. Reading Error Messages

  • Python and most languages provide error messages and stack traces.

  • These messages tell you what went wrong and where.

Example:

numbers = [1, 2, 3]
print(numbers[5])

Output:

IndexError: list index out of range
  • This tells you the error type (IndexError) and the line number causing it.


2. Using Print Statements

  • Inserting print() statements helps track variable values and program flow.

Example:

x = 5
y = 0
print("x:", x, "y:", y)
result = x / y # Will cause ZeroDivisionError
  • You can see exactly where values might cause errors.


3. Step-by-Step Execution

  • Break your code into smaller parts and run them one by one.

  • Helps isolate the exact line where the bug occurs.


4. Rubber Duck Debugging (Fun Trick!)

  • Explain your code line by line to an object or a friend.

  • Often, just verbalizing the problem helps you spot mistakes.


Page 3: Debugging Tools

1. Python Debugger (pdb)

  • pdb allows pausing code, inspecting variables, and running commands step by step.

Example:

import pdb

x = 10
y = 0
pdb.set_trace() # Pause execution here
result = x / y

Commands inside pdb:

  • n → Next line

  • c → Continue execution

  • p variable → Print variable value


2. IDE Debuggers

  • Most IDEs like PyCharm, VS Code, or Thonny have built-in debuggers.

  • Features include:

    • Setting breakpoints

    • Stepping through code line by line

    • Inspecting variable values at runtime


3. Logging

  • Instead of using print(), you can use the logging module to record events.

  • Useful for large projects where print statements become messy.

Example:

import logging

logging.basicConfig(level=logging.INFO)
x = 5
logging.info(f”x is {x}“)


Page 4: Strategies for Effective Debugging

1. Understand the Problem

  • Carefully read the error message.

  • Know what the code is supposed to do.

2. Isolate the Problem

  • Remove unnecessary parts of code to focus on the error.

  • Test smaller sections individually.

3. Check Assumptions

  • Sometimes errors occur because your assumption about data or logic is wrong.

Example:

numbers = [1, 2, 3]
if len(numbers) > 3:
print(numbers[3]) # Bug! List has only 3 elements
  • Assumption len(numbers) > 3 is false. Debugging identifies this.


4. Use Version Control

  • Tools like Git allow you to track changes and rollback if something breaks.


5. Be Systematic

  • Change one thing at a time.

  • Test frequently to see if the bug is fixed.

  • Document what you tried to avoid repeating the same steps.


6. Example of Debugging a Small Program

def divide(a, b):
return a / b
try:
print(divide(10, 0))
except ZeroDivisionError as e:
print(“Caught an error:”, e)

Debugging Steps:

  1. Run program → observe ZeroDivisionError.

  2. Add try-except → catch the error and handle it.

  3. Test with different values → ensure program works correctly.


Summary

  • Debugging is an essential skill in programming.

  • Techniques include:

    • Reading error messages

    • Print/log statements

    • Step-by-step execution

    • Using debuggers (pdb or IDEs)

    • Rubber duck debugging

  • Effective debugging requires patience, logical thinking, and systematic testing.

Debugging is like being a detective in your own code: following clues, testing hypotheses, and solving mysteries until everything works perfectly.