Hello World: Your First Raspberry Pi Project

A gentle introduction to using Python with the Raspberry Pi

Hello World Terminal Print
Image: Richard Saville

When you're new to the Raspberry Pi it can be all too tempting to try and jump straight into the projects that attracted you to the device in the first place.

Robots, sensors, music players and similar projects are great uses for the Raspberry Pi, but not the best start for someone new to the device. In an ideal world, you should aim to learn the basics before charging into a complex project.

If you're also new to Linux it can be an even steeper learning curve, so it's best to start off with simple projects to familiarize yourself with how Python works, and then build on that knowledge over time.

A Gentle Introduction

One of the most common first projects on a Raspberry Pi is to print the text "Hello World," either to the terminal with a script or using the IDLE Python development environment.

It may seem like a boring start, but it gives you an easy and relevant introduction to Python - and it's also a function you'll be using lots in your future projects.

Let's go through a few variations of this traditional lesson to really kick off our programming skills with the Raspberry Pi. We'll be using python scripts instead of IDLE, as that's simply our preferred method.

Hello World

Let's start at the very beginning with the basic printing of the text "hello world".

Once logged in to a terminal session, enter the command below to create a new python script called 'helloworld.py'.

Nano is the text editor we will be using, and 'py' is the file extension for Python scripts.

We also use sudo (which stands for 'superuser do') at the start which runs the command as a superuser. You don't always have to use this, and it can be dangerous in the wrong hands with the wrong commands, but we tend to just use it as a habit now.

This command will open a new blank document. Enter the text below which will print the term "hello world" when the file is run:

Once entered, press Ctrl+X and then hit 'Y' to save the file. The terminal will ask you to press enter to save the file with the specific file name, so go ahead and hit the enter key. You've just created your first Python file!

You will now be back in the terminal. To run our new script, we use the command below:

This will print "hello world" and then close the script, leaving us with the terminal to use again.

Hello Then World

Time to go up a gear. This example will print the word "hello" on one line, and then "world" on the next. This will add a new line to our Python file, but still at a very simple level.

Start a new file by using the command below:

Once again this will open up a blank editor window. Enter the following text:

Again use Ctrl+X to exit and save, then pressing 'Y' and then 'enter' when prompted.

Run the script with the following command:

This will print "hello" on one line, "world" on the next line, then close the script.

Hello World, Goodbye World

Using what we learned in the previous example, let's change things so we say "hello world" then "goodbye world" over and over until we tell it to stop.

You've learned how to make and use files so we will skim over those instructions this time.

Make a new file called hellogoodbye.py and open it in nano. Enter the following text:

We have introduced a few new concepts here:

  • Import: The import line brings in the time module which allows us to ask the file to wait for a period of time where we specify. We do this with 'time.sleep(1)' later in the file.
  • Count: The count line is defining a variable, which is just a way of holding a value and giving it a name. We start this at '1' which we will explain further down.
  • While: We have also used a while loop. As the name suggests, this 'loops' our code 'while' a condition is true. In this case, we're using 'while True' which just means it will run forever until we stop it manually.
  • If statements: Within our while loop we have a couple of 'if' statements. The first statement says "if count is exactly 1, carry on and run the code indented below". Indentation is important in Python for functions like this.

If this indented code does run, it will print "hello world" and then change our 'count' variable by -1. It will then wait for a second with 'time.sleep(1)' before cycling back to the 'while loop' to run again.

The second 'if' statement does a similar job but only runs if 'count' is exactly 0. It will then print "goodbye world" and add 1 to 'count'. Once again it will wait a second before running the 'while loop' again.

Hopefully, you can now see how 'count' starts at 1 and will constantly cycle between 1 and 0, printing different text each time.

Run the script and see for yourself! To stop the script, just hit Ctrl+C.

Hello World 100 Times

How about repeating our text print just 10 times, automatically? This is achieved by using the count within a while loop again, but changing how we manipulate it.

Create another new file, give it a name, and then enter the text below:

Here we have used '<=' in the first 'if' statement which means 'less than or equal to'. If the count is less than or equal to 10, our code will print "hello world".

The next 'if' statement looks for number 11 only, and if the count is at 11 it will run the 'quit()' command which closes a script.

Try the file to see this for yourself.

Over To You

These exercises show you some very basic ways of manipulating code, but it's the kind of foundation learning that all new Raspberry Pi and Python users should get to grips with early on.

If you haven't found it already, check out Lifewire.com's dedicated Python site to learn more about this excellent programming language.

We'll cover more code examples in future articles and projects, stay tuned!