I thought it could be worth while to go through a quick “first program” using Thonny. If you didn’t see my last blog post, Thonny is a python code editor designed for people brand new to programming. You can download it here: https://thonny.org/
It’s easier than it looks.
The first time you open up Thonny, you get a pretty simple window with two mostly empty panels. The upper panel is where you write your code, and the lower panel is where you see the results of your programs and interact with them while they are running. So, let’s write our first program.
In the upper panel, write the line below, exactly as it is written.
print("It works!")
That’s it! You’ve written your first program. To run it, click the green circle with a white triangle inside it (the fourth icon in the row of icons above the upper panel). In general, this will simply run your program, but the first time you click it on a new file, it will ask you to save the file first. I recommend creating a new folder either on your desktop or in your documents folder, where you can save all of your Thonny projects. For simplicities sake, let’s call this “project1” and then click the “Save” button in the lower right corner of the save window.
Thonny will then save the file and run the program. When that happens, the lower panel should look very similar to this:
If you don’t see the words “It works!” in the middle, but see something in red instead, there was probably a typo in your code. Go back and double check that it is exactly like I typed it above (you can even copy and paste if you need) and then click the green Run button again.
What’s going on here?
There are two important things to know about what we’ve just done.
Firstly, we used a function called print(), which prints onto our screen whatever we put in between the brackets. Functions are like micro-programs that only do one thing. Most programs have many functions in them. We will make our own functions at some point, but most programming languages come with many functions built-in (the print() function is built into Python, which is the name of the programming language we’re using in Thonny), and some functions are written
Secondly, the programming term for what we wrote between the brackets is called a string. Strings are like normal text/words. To write a string, you just need to put everything you want to be part of that string between two quotation marks. You can use double quotes, “like this,” or single quotes ‘like this.’ But whichever you use at the start of the string has to be the same as what you use at the end.
So, in the end, all we did was tell our computer to print the string “It works!” onto our screen. Simple. No, it’s not very exciting, but we’re just taking this slow, to make sure we can get a clear understanding of each step of the process.
Tomorrow (or soon after), we will tackle something called variables.