Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Debugging

Give Feedback

00:00 Welcome to this section about debugging in Thonny. You’ll see that Thonny is a cool tool for getting started, especially through the debugging feature that it has built-in.

00:10 Here’s the code that we wrote before, and now instead of just running it, I’m going to press the green bug symbol because we want to debug this little script that we wrote. Okay, so once I press this bug, we can see that the code that it’s currently looking at is getting highlighted in yellow. Down here in the shell, we see that we’re debugging this script file that we wrote. Okay.

00:32 So now it can step over, and in this case, this is not going to give as much because it just jumps over the whole block and gives us the output. Not very interesting.

00:41 If you have a different structure and bigger blocks, then it’s going to just take you to the next bigger block. And now the interesting part. If we go inside, so we Step Into, it’s going to evaluate every piece in itself and show us the output.

00:56 I really, really love seeing this feature because it’s a great starter feature. Here, it’s highlighting the code block that it’s currently looking at, so it’s looking at range(5). And I’ll show you by pressing forward, it’s telling us—okay, now it’s looking at 5.

01:09 What’s it going to do with that? All right, it puts this in there…

01:14 and we can see that this is what it actually creates, because we know that range() has this option of giving a start and an end, and if you don’t put a start, then it just takes 0 as the start by default.

01:26 And here we can actually see that this happens. So it evaluates in this way that it’s looking at the blocks—range(5) and then—this is what it comes up with, this is the result. It’s going to be a range that starts at 0 and goes up to 5.

01:40 Up to but not including 5. So, we continue stepping. Now we know that the first thing it’s looking at is the 0. If I keep stepping, it’s going to take a look at this expression here—it’s going to start evaluating it.

01:53 And for x, it gets replaced with 0 because that the first thing that we’re taking out of this range() up there, and it’s going to say 0 to the power of 2.

02:05 Oh here, it was checking, like, is this a variable, or is it already an integer? When it turns blue then it has the return value, so to say. Okay, so, and this returns 0.

02:16 Here we can get what it returns—in this case, that’s still 0.

02:20 And now we’re in the print() function and we start to evaluate this one. xokay, it’s looking at x, and we know that x returns 0, x refers to 0.

02:31 So that’s what’s happening here. Now we’re executing print(). Down here we see in our shell, 0 gets printed out. And the return value of this is None because the print() function returns None.

02:43 And that’s one full loop through this. Now, if you keep going, we’re at the next one. range() has already evaluated the next value that it has, that it looks at—it’s 1. So we’re going to go through the same thing again, with 1 as the value for x.

02:58 Start evaluating this one. We’re looking at x first. x gets replaced with the value 1. We’re looking at 2—that’s actually 2. 1 to the power of 2 equals 1.

03:10 That’s our return value that’s assigned to x. Then we move forward to print(). We’re looking to evaluate this one, x. We’re looking at x, what’s that? That’s 1.

03:20 Print it out. It appears down here, and our return value is None. So, this is a great way—I’m just going to step through this quicker. You already understand the point of it.

03:31 But you can see at every step in the program what’s happening, what is it evaluating, what does it return, what’s the value at the moment—and this is just visually right in your code, and very easy to understand what’s happening through these highlights and step-by-step process.

03:47 I really like this because I think it’s a great way of starting to debug, as well as if there’s actually a bug in your program or also just in a way that I’m doing it now, to step through your program and understand what is happening in there at each step. So yeah, this is a very, very helpful feature in Thonny, and I would very much suggest it for beginners to do this a lot.

04:09 Just get familiar with debugging, think of it as a tool to step through your code and figure out what’s actually going on. It helps you get into the process of thinking about what’s happening in this code.

04:20 Like here, we have a focus on something and it evaluates to something, another focus on something—this expression—it evaluates to something. And just this way of thinking, seeing it visually in this debugger, is extremely helpful. Okay, so I’ve talked enough about this now. That’s debugging in Thonny.

04:37 There’s more that you can learn about it, obviously, but I hope this is going to give you a good idea of what’s possible and maybe entice you to get started working with this, especially when you’re a beginner, but also actually at any level it can be very helpful to visually debug your code. All right! In the next video, we’re going to look a little more at some other useful features that Thonny has to offer. See you over there.

Become a Member to join the conversation.