// tech tips · beginner

Debugging Python in VS Code: Breakpoints and Stepping Through Code

Stop guessing and see exactly what your code is doing. Learn to use breakpoints, step through Python line by line, watch variables and find logic bugs with the VS Code debugger.

Some bugs crash your program with an error message, and the error messages guide shows you how to fix those. The trickier bugs give no error at all: the program runs, but the answer is wrong. For those, adding print() lines helps, but a debugger is far more powerful. It lets you pause your program on any line, look inside every variable, and run the code one step at a time.

This guide uses VS Code with the Python extension installed. If you haven’t set that up yet, follow Set Up VS Code first.

A program with a hidden bug

Save this as average.py. It’s supposed to print the average of the numbers, which is 6.0:

python
def average(numbers):
    total = 0
    for n in numbers:
        total = n
    return total / len(numbers)

scores = [4, 8, 6]
result = average(scores)
print(f"Average: {result}")

Run it and it prints Average: 2.0. There’s no error, just a wrong answer. Can you spot why? Let’s use the debugger to find out.

Step 1: Set a breakpoint

A breakpoint tells the debugger “pause here”. Hover just to the left of the line number for line 2 (total = 0), and click the faint red dot that appears. It turns solid red.

You can also put your cursor on the line and press F9.

Step 2: Start debugging

Press F5 (or open the Run and Debug panel on the left and click Run and Debug). If VS Code asks what to debug, choose Python Debugger, then Python File.

The program starts and pauses on line 2, which is highlighted in yellow. The highlighted line hasn’t run yet; it’s the next one to run.

Step 3: Look at the variables

On the left, the Variables panel shows every variable that exists right now. Under Locals you’ll see numbers = [4, 8, 6]. You can also hover over any variable in your code to see its value.

Step 4: Step through the code

A small toolbar appears at the top of the window. These are the buttons you’ll use most:

Button Shortcut What it does
Continue F5 run until the next breakpoint (or the end)
Step Over F10 run the current line, then pause on the next one
Step Into F11 if the line calls a function, go inside it
Step Out Shift + F11 finish the current function and pause after it returns
Restart Ctrl + Shift + F5 start again from the beginning
Stop Shift + F5 end the debugging session

Press F10 a few times and watch the Variables panel as the loop runs:

Line n total
after first loop 4 4
after second loop 8 8
after third loop 6 6

There’s the bug: total is being replaced each time instead of added to. Line 4 should be total = total + n (or total += n). Stop the debugger, fix the line, and run it again: Average: 6.0.

That’s the whole skill: pause, look, step, and compare what you see with what you expected.

Step Into vs Step Over

When the paused line calls a function, such as result = average(scores):

  • Step Over (F10) runs the whole function and pauses on the next line. Use it when you trust that function.
  • Step Into (F11) jumps inside the function so you can follow it line by line. Use it when the bug might be in there.

If you step into something by accident, Step Out (Shift + F11) gets you back.

Watch expressions

The Watch panel shows the value of any expression you type, updated at every step. Click + and try total / len(numbers) or n * 2. It’s like a print() you can change without restarting the program.

The Debug Console

While paused, open the Debug Console (it’s next to the Terminal tab). You can type Python there and it runs in the paused program, using its current variables:

python
>>> numbers
[4, 8, 6]
>>> sum(numbers) / len(numbers)
6.0

It’s a great way to test a fix before you change your code.

Conditional breakpoints

In a loop that runs 1,000 times, pausing on every pass is painful. Right-click a breakpoint, choose Edit Breakpoint → Expression, and enter a condition such as:

python
n == 8

Now the debugger only pauses when that condition is true.

Debugging programs that ask for input

Programs that use input() work in the debugger too. When execution reaches input(), click into the Terminal panel and type your answer there.

The debugger isn’t always the right tool. For a quick check, a temporary print() is often faster:

python
print("DEBUG total =", total)

Use print() for a quick look at one value, and the debugger when you need to understand how values change over time. Remember to delete your debug prints once the bug is fixed.

A debugging checklist

  1. Reproduce it: find exact input that causes the wrong result.
  2. Predict: decide what each variable should be at each step.
  3. Pause before the problem: put a breakpoint a little before where you think it goes wrong.
  4. Step and compare: the first place reality differs from your prediction is where the bug lives.
  5. Fix one thing at a time, then run the program again to confirm.

Key points

  • A breakpoint (click left of a line number, or F9) pauses the program there.
  • F5 starts debugging; F10 steps over a line; F11 steps into a function.
  • The Variables panel, hovering and the Watch panel show values as you go.
  • The Debug Console runs Python inside the paused program.
  • Conditional breakpoints pause only when an expression is true.

Debugging is a skill that improves every time you use it. Next time something gives the wrong answer, reach for F9 and F10 before you start guessing.