Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Step Through Simple Scripts

The buttons you’ll see in this lesson allow you to debug and run a script step by step in order to see what’s happening. These step buttons are really powerful because they allow you to see what’s going on under the surface.

A large part of learning programming is really understanding what the computer is actually doing because it’s very easy to think that you’ve told it to do one thing when it’s in fact doing something else.

00:01 Stepping Through Simple Scripts.

00:06 The next buttons are linked to each other. They allow you to debug and run a script step-by-step to see what’s happening.

00:15 There are three types of step buttons here, and we’re going to look at how they work in a fairly simple example.

00:23 They give you really great power because they allow you to see what’s going on under the skin. A large part of learning programming is really understanding what the computer is actually doing, because it’s very easy to think you’ve told it to do one thing and have your program doing a particular task, when it’s not doing exactly what you think.

00:42 There may be some situations where it’s working exactly as predicted but then another time you might have a different input, different numbers running, et cetera, and then suddenly it won’t be doing what you think at all and it can be pretty confusing.

00:57 So being able to look inside what the computer is doing as it’s executing and seeing what variable values are, et cetera, with Python is really, really important.

01:07 This is a great aid to your understanding and learning of programming generally, as well as specifically fixing bugs that you create. And rest assured, everybody creates bugs.

01:20 So, now we’re going to check this out with a simple program with some variables in. Here you can see… I’m just going to close that other program. So, here you can see it’s a fairly simple program. It’s got x starts out as 1 and then we have this loop with a range(). Now, if you’re not sure what that is, there are other videos and tutorials on Real Python which will allow you to understand that and really get that down.

01:44 And then each time through this loop, it adds the value of i to x. So the first time around it’s going to be a 0 because the range iterator starts from 0.

01:56 The second time it’s going to add a 1, then it’s going to add a 2, and so on. And if we run it, it runs in a blink of an eye, and we don’t even see anything because we haven’t told it to print anything out.

02:06 Let’s try printing that out, so we’re going to print out x at the end. Run it again. It runs very quickly. We get the result 46, but we don’t really know what’s happening in the background for sure.

02:18 We’re not party to any of the internal workings. And the way we’re going to do that is we’re going to use debug. And we’re also going to open up the Variables window, which is found under View.

02:29 So you’ll see on the right-hand side of the screen now, we have the final values of i being 9 and x being 46.

02:39 So at least we have some idea of what’s happened within the program, but now what we’re going to do is step through and see those values during execution. So we’re going to slow time down.

02:51 It allows us to pause it at particular points.

02:54 So using the debug tool, we can step through the program and take our time—as much time as we want—to understand what’s happening at each step of the program.

03:03 So we click Debug or Control + F5. It’s currently executing line 1. So as we do Step into We’re not going to use Step over because it can do too large a step.

03:18 We’re just going to do every single step that it does, so it gets the value 1, and then it assigns that to x. We press it again.

03:25 We can see on the right-hand side, x has now got the value of 1 assigned to it. It’s stored in Python’s memory. And now we step into the next bit, and it looks at the range iterator, which we’ve given the value of 10.

03:39 And then we keep stepping. It’s going to expand that to range(0, 10) because the shorthand of range(10) actually has a start value of 0.

03:49 And then as we step further, we can see i is now 0. And now it gets to the line where it says x is 1, but we can see on the right that i is 0.

04:02 And now it’s going to add i, so it looks up the value of i, which is 0, and now it’s going to add that to x.

04:11 And as far as we’re concerned, nothing happened because x didn’t change because we added 0 to it. This time around, i is equal to 1. So again, it looks that up. And now on the next step, we can see x is equal to 2.

04:30 Now, this next step, i is equal to 2, we can step through it again. x is equal to 2. Now x is equal to 4, i is equal to 3. So it adds 3 to x, so x ends up at 7. And so on. So, we can see all of those steps, or if we’re happy with the way it’s running, we can press Resume and it will just go as fast as it can to the end.

04:52 It finishes it very quickly.

04:55 We’ll look at these other buttons in a bit more detail later on with a more complicated program, but let’s run that again. We’ll just use the debugger, get a few steps into it, and now we can decide we want to stop.

05:07 So if you’ve ever got a program which is too large, and you’ve learned what you need to know, et cetera, you’ve found your bug, you can just hit Stop.

05:15 And you can see there, we dropped back into the editor and the program has stopped in the shell.

05:24 So, that’s the main buttons there. We’re going to run through those with some more detail in a little with a more complicated program, but those are the main controls that you’re probably going to be using most of the time when using Thonny. And the Variables window can be really useful, as you’re going to see in an example a bit later on!

Become a Member to join the conversation.