Python Fundamentals

Debugging Strategies in Python

Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "Debugging Strategies in Python" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to debug a python file using print statements.

Preview
Close

Transcript from the "Debugging Strategies in Python" Lesson

[00:00:00]
>> Nina Zakharenko: Generally, you'll want to debug your code with print statements when you're first starting out. So just an example of that, I'm gonna make a new file and I'm gonna call it mystery.py. And I'm gonna make a function called mystery.
>> Nina Zakharenko: And I'm gonna say that my number is equal to this.

[00:00:29]
And if my number is equal to 10, let's set it to the number times 10.
>> Nina Zakharenko: Otherwise, if the number, and I can use an l, also, here as well. Otherwise, if the number is equal to 30, we're gonna set the number to the number times 30.
>> Nina Zakharenko: And we're gonna return that number, let me hide the terminal for a second.

[00:01:07]
And then at the bottom of the file we're gonna call this mystery().
>> Nina Zakharenko: And notice that calling the function is not indented under the function definition. So calling that real quickly. Oops, I wanted to print the result of calling mystery. We'll see that the number was 900 and it's like well I'm not really sure what happened here right?

[00:01:34]
So we can put some print statements here.
>> Nina Zakharenko: And that will help us figure out which path we went down, right? You can say num was here.
>> Nina Zakharenko: And I'll add these particular files to the Git repo so that you can look at them. Now if I run this code again, well I know which condition was met.

[00:02:05]
I know what the number was set to, right. Generally, as you advance in Python, you're gonna want to use a debugger. But for the time being, print statements are a great tool. But you just have to remember not to litter your production code with them, right?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now