A gentle introduction to print statement debugging
Published: 2024-07-02 11:40:34

This [blog](https://www.letterstocoders.com/p/a-gentle-introduction-to-print-statement) was originally published on Substack. Subscribe to ‘[Letters to New Coders](https://www.letterstocoders.com/)’ to receive free weekly posts.

If writing code is day, then debugging is night. Dealing with bugs is non-negotiable in coding. In fact, the most skilled programmers are also the best at debugging.

Today, I want to give a gentle introduction to one of the most effective debugging strategies that will be relevant throughout your career: print statement debugging.

If you have already written an entire program, then you’re ready for this tutorial. If you haven’t, feel free to join us and get a sneak peek of this skill that’s waiting for you later in your learning journey.

## What is print statement debugging?
Print statement debugging is a fantastic way to trace the behavior of a program to understand where an error occurred, and how to fix it. (Another perk: it requires no additional tools or complex setups.)

This debugging technique involves placing print statements at various positions in your program.

These print statements help elucidate your code’s behavior by generating outputs that reflect:

- The values of variables
- Flow of execution
- The state of your program at various points
These print statements are additional lines that you write into your program. If you owned a coffee shop, you can think of print statements as shift supervisors who are present at the shop, keeping you updated on the status of operations.

Of course, print statements are not placed randomly throughout code. You should place statements strategically at **key areas where the state changes or important operations occur**, such as:

- Before and after loops
- Inside conditional blocks
- At the beginning, middle, and end of functions
For example, suppose we have written a program that calculates the sum of numbers in a list but it’s not returning the expected result. Here’s the code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajfjgl012hm65c7wq7pi.png)

We can add a print statement in the code to help us notice what’s happening in the program.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw12q298jfnrbn5c1b3z.png)

We can add a print statement in the code to help us notice what’s happening in the program.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyz7s5pjldi3tnop17tm.png)

This added print statement will show us the value of the number being added and the intermediate value of the total. This helps us trace the execution and verify if the logic inside the loop is correct.

## Using print statements for debugging

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehkrgp8x88upnyraboqa.png)

In its simplest form, the steps to using print statements for debugging are:

1. Identifying key areas
2. Inserting print statements
3. Running the program
4. Analyzing the output
5. Identifying the issue
6. Fixing the bug
7. Removing the print statement

Here are some best practices to keep in mind:

## Use descriptive messages
Instead of just printing variable values, include context about what those values represent. For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axc042m90d98v86yfsck.png)

## Track execution flow
Use print statements to trace the flow, especially in complex logic with multiple branches. For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4uijkixvfr7tfw5du313.png)

## Be strategic (don’t overdo it!)
Overdoing print statements can affect the performance of your application — especially when you’re working in the professional environment.

In professional development, you’ll be working on massive codebases where performance is likely a priority. Instead of using print statements, you may be using a related technique called logging, however you’ll still need to decide where to place logging to help monitor a program. Figuring out exactly where to put print statements without overdoing is not so much a science as it is an art. But the more you do it, the better your judgment will get.

## Example: Code with a logical error
Let’s try with an example.

Suppose we have a function that checks if a list of numbers contains any even number(s). The code is intended to return True if there is at least one even number and False otherwise.

However, there is a logical error:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/639azruu6cp1cf1dda2f.png)

Expected output: True
Actual output: False

To debug this, we can **add print statements** to understand the flow of execution and the values being checked:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7s0aqx9xs8vw8jvsm1o.png)

## Analyzing the output
By running the modified code, we get the following output:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vk6b69wmv55q9jmj22p2.png)

The output shows that the function returns False after checking only the first number, 1. This indicates that the else branch is causing an immediate return of False, even if there might be even numbers later in the list.

## Corrected code
To fix this logical error, we should only return False after checking all numbers in the list:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4p322jhvdv23i497rnu.png)

## Corrected output
Now, the output correctly identifies that there is an even number in the list:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2egm0sbjthnqbhoobbm.png)

Using print statements, we traced the execution flow and identified that the function was returning False. This allowed us to correct the logic and ensure the function worked as required.

## Becoming a professional debugger
Becoming a developer means becoming a debugger too. And print statements are a great way to start getting methodical with debugging. Once you’ve understood print statements and strengthened your debugging skills, you’ll be able to move on to logging for the professional development environment.

Either way, the best way to grow your debugging skills is by making mistakes and getting your hands dirty with debugging.

If you’d like to learn more about debugging, we’ve launched a great beginner-friendly course that covers various debugging strategies: [Mastering Debugging: Techniques for Efficient Code Diagnosis](https://www.educative.io/courses/mastering-debugging-techniques-for-eficient-code-diagnosis).

Happy debugging!

– Fahim