Hi All,
In this blog post I go through the very first basic steps of programming a Raspberry Pi using Python, to run the familiar starting program "Hello World"
Note that this is geared for people who are complete beginners with Raspberry Pi.
My first thought was that this must have been done a zillion times before, but when I started looking I couldn't find anything that summed it up in easy steps, and I found myself having to dig a little just to get the first steps figured out. Not too difficult, but a bit annoying. So I thought I may as well track what I'm doing and make this blog post, in the hopes that it might make someone else's life a bit easier.
First of all, I'm assuming that your Raspberry Pi is already set up, and that you are using the Graphical User Interface. I'm using Raspbian here, and all my steps will be based on that. I'm also using the version of Raspbian that came on a NOOBS SD card, circa October 2014. So if your version is older it may not have all the tools installed in the same way. In that case, please refer to the many other resources out there that can do a much better job than I of explaining how to update and/or install the software.
If all else fails, it might be easiest just to get a new SD card and install the latest version of Raspbian on it.
I am doing this on a Raspberry Pi Model B+, but everything should work the same way on other models.
The tool we will use is "IDLE 3", which is a handy little Python editor. It comes pre-installed with Raspbian.
The first step is to just double-click the IDLE 3 icon to open the "shell" - basically a line by line Python interpreter.
Now you can try your very first line of Python: at the >>> prompt, type this line:
print("Hello World!")
then press the enter key. The shell should respond by printing "Hello World!" on the next line and then giving you a new >>> prompt.
The shell provides a nice and easy way to try single line Python commands, but most programs require more than one line. To accomplish this, you need to either open a file, or create a new file.
Let's create a new file: from the file menu of the Python Shell window, choose "New Window".
A new window titled "Untitled" will pop up on your desktop, and it'll be a blank slate. We'll just ignore the obvious self-contradiction of the title of the window. But this is where you can now start typing in your program.
Keeping with the super-easy theme, let's also make it say "Hello World!", by typing the same line as before, but this time type it into the Untitled window.
You'll notice that it won't automatically try to run it. That's the point - it gives you time to type out a more complex program before it tries to run it.
To run it, we'll need to save the file first. The usual ctrl-S or File-Save will do the trick. Put it somewhere where you'll be able to find it back later, of course. I prefer to put it under "Documents", as I'm not using this Raspberry Pi computer for anything else.
Now from the file menu, select Run and Run Module to run your new Hello World program.
You'll notice that it switches the window selection to the Python Shell, and then prints a long line that says "RESTART", shows a prompt, then shows your "Hello World!" line, and then another prompt.
That's it! That's all there's too it.
Now the next time you open IDLE 3, you can simply use "File-Open..." to open the existing helloworld.py file and run it again.
I hope that helps out. My next blog post will go on to show how to blink an LED - the basic microprocessor version of "Hello World"
Edit: This has been tested and works on both the RPi model B+ and the RPi 2 model B.
Cheers,
-Nico
Top Comments