Creating and Running a Code Snippet - Thonny
00:00
In this video, we’re going to write our little code snippet—the one that we’re going to use across all the editors—and see how this looks like when we’re doing it inside of Thonny. Over here, I made a new file. I called it script.py
and saved it in the same way we did just in the previous video. And now I can write my code snippet in here.
00:19
It’s going to be for x in range()
, and you see the nice code syntax highlighting that happens automatically. And I want to show you also that autocompletion is a thing in here. If I just type r
and then press the Tab character, it gives me specific options that I have that start with an r
.
00:37
I can go through them with the Up and Down arrow keys. In this case, I want range()
. And another way to do this also is to press Control + S, and it brings me to the same thing as the Tab character, and you also saw that the menu flashed up here.
00:53 So we have Edit and then there’s some Auto-complete feature down here, so you can also select it through the menu. Auto-complete, and it opens up these options. The simplest is the Tab character, I just suggest you to get used to it because most code editors use the Tab character for autocompletion.
01:10 So, I can just press Enter once I have selected it and it writes for me the whole thing.
01:16
So now I want to say 5
times, colon (:
). I press Enter, we can see it automatically formats it, that it indents these four characters and I don’t have to go duh-duh-duh-duh with the space character. Instead, I can just continue writing here. I say x = x**2
(x
squared). Again, you can nicely see that we have pretty syntax highlighting. We can right away see what’s a variable, what’s a reserved keyword, what’s an integer, et cetera.
01:46 All right, print it out. Here we go. And now I can run this by just clicking this button up here or running F5. I’m just going to click the button. And in our shell, we can see the output. You can adapt the window sizes easily if you want to move that a bit up. I changed the font size also. Okay, anyways, so here’s our output. It’s all integrated.
02:08 We have the editor where we’re writing the script, and then we have the shell where we can see our output. And in this case, we can also just continue. We see the output and we can continue typing something.
02:18
So, I can say x = 2
and then x**2
to check whether this is correct, yeah, ha.
02:26 We got the right calculation. Sweet! So that’s the simple Getting Started in Thonny. You write your scripts, you can run them using this button, and you can see the output down here, plus you can use the shell also additionally to, like, investigate a bit more and just write some code where you want to see the results right away.
02:45 So it’s nice to have this combination going on in here. Okay! That’s the very simple version of just writing this, and that might be enough for you if you’re getting started.
02:54 I will show you in the upcoming sections, we’re going to take a look at debugging, which as you can see by the default command line up here, there’s one, two, three, four, five of the symbols that are up here are dedicated to debugging, which means that debugging has a big focus in this editor.
03:13 And it’s done very well, which is great for getting started, because you want to understand what’s going on in your project. That’s going to make learning much easier.
03:20 We also saw that we have the Assistant—there it is—that we can switch on and off, that before gave us some info when it was an error. This—if I try to run that—
03:34 got a traceback and useful information on what we can do. Python doesn’t know how to read your program. And then we can see, like, there’s more hints here.
03:43
The little arrow symbol (^
) in the original message tells us that didn’t work, but a mistake happened either there or somewhere before. I see the point there, okay. So just really teaches us in a nice way how to interact with error messages, and also how to get familiar with working with an IDE, because we have this same setup here.
04:02 We can switch those different tabs on and off. I would suggest you to experiment a bit with View. For example, we can switch on a file view on the side, it just gives us a normal overview where we can—
04:16 ah, this is probably a bit big.
04:18 All right, we can use it as a normal file view—switch it on or off, and have a very simple environment or extend it to look very similar to a complex IDE. That’s all for now!
04:29 I’m going to stop this video, and we’re going to start talking about debugging in the next one, and you’re going to see there are some pretty cool features that Thonny has in store for us. See you there.
Become a Member to join the conversation.