Creating and Running a Code Snippet
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.
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 all that autocompletion is the 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
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.
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 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: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: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 is a big focus in this editor.
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: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.