It’s customary in the programming world that the very first program you write is called, “Hello, World!” The purpose of this program is simple – have the computer print the words, “Hello, World!” on the screen.
We’re not ones to buck tradition, so we’re going to make “Hello, World!” our very first project. The nice thing about this project is that it’s easy to do (it’s only one line of code) and it will give us an opportunity to start exploring our RaspberrySTEM Development Environment (RDE).
Programming is the process of writing instructions for a computer to follow. You write these instructions using an editing tool (we provide a custom editing tool as part of the RaspberrySTEM CREATOR Kit) and then "send" those instructions to the the computer to carry out (or "execute"). Just like there are lots of different languages that people can speak, there are also many languages that computer instructions can be written in -- each of which will ultimately be converted into a format that the computer understands.
The RaspberrySTEM CREATOR Kit supports many different programming languages, but we will primarily be focused on one of the most common, called Python. Python is a versatile language that is used in both the academic and the professional world to build a varied set of applications and tools. Learning how to program in the Python programming language will provide a solid foundation for all future programming education.
Here are step-by-by instructions for implementing this project; after we’ve completed it, we’ll talk more about how and why it works.
All the code you will be writing as part of the RaspberrySTEM projects will be done within the RDE. Specifically, you’ll be writing your code in the Code Window portion of the RDE. As a reminder, here is where the Code Window is located:
You’ll notice that at the top left corner of the Code Window, there is a line numbered “1”. This is where you will type in your program’s code:
It's now time to enter the code for this project. In the code window, enter the following:
A few things worth noting at this point:
The line number "1" will automatically be visible before you start entering your code
In future projects, the code boxes we present may or may not indicate line numbers next to the lines of code. Whenever possible, we'll include line numbers to make it easier to ensure that your code is identical to ours; but, in some cases, we'll leave out line numbers to avoid confusion -- this is typically when we're adding new code to existing code and we can't be sure our line numbers will be the same as yours.
Capitalization generally DOES matter when you’re programming. In this line of code, if the word “print” isn’t in all lowercase letters, you may find that your code doesn’t work as expected.
We mentioned in the Projects Guide introduction that there's a way to cut-and-paste code instead of typing it all in. As an example of how you do this, you can cut-and-paste the line above right into your code window. To do that, follow these steps:
Highlight the print ('Hello, World!') line in the code box above using your mouse
Press and hold the <CTRL> key on your keyboard and press the letter "C" at the same time -- this copies your text
Click in the Code Window where you want to paste the code
Press and hold the <CTRL> key on your keyboard and press the letter "V" at the same time -- this pastes the text
Now that you’ve
typed (or cut-and-pasted) the line of code into the Code Window,
let’s see if it works. We do this by “running” the program. To run the
program, click on the Play icon ()
at the top of the Code Window.
The program runs by starting at the first line, doing what that line says (“executing” that line), and then moving on to the next line. It does that until it reaches the bottom. In this case, we only have one line, so only that line of code is executed.
It may take a few seconds for the code to run, and you may notice two things during this time:
The the triangular PLAY icon turns to a square; and
That the little RaspberrySTEM icon at the top of the code window spins.
These are just indications that the Raspberry Pi is busy running your code. And don't worry if you don't notice these things -- sometimes they happen so fast they they're not very noticeable.
If you’ve done everything correctly, you should see the following in your Output Window (the window in the lower left of the RDE, below the Code Window):
Hello, World!
-- PROGRAM FINISHED --
The first line of the output window is the result of the code you’ve written – you’ve successfully had the computer print the words “Hello, World!”
The last line indicates that the program is finished running – this will be helpful later when you’re writing longer and more complicated programs.
Congratulations! You’ve just written your first program!
But wait! What if the output from your program doesn't look like it's supposed to? Or if you get a jumbled red and black error-looking message in the Output Window? If that's the case, you likely have an error in your code. But, even if your code worked perfectly and you didn't get any errors in your code above, we highly recommend reading the next section, as you're certain to encounter programming errors at some point: