Hello World: Your First Raspberry Pi Project

A gentle introduction to using Python with the Raspberry Pi

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!"

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

    touch helloworld.py

    The Linux command, 'touch,' lets you create a new blank document. You're giving it a name of "helloworld" and providing the '.py' extension to mark it as a Python script file.

    Create Python script Raspberry Pi
  2. Now, open your new file with Nano. Nano is the text editor we will be using. It's a very simple editor that comes with nearly all Linux systems. Open up your new Python file using Nano.

    Open Python file with Nano on Raspberry Pi
    nano helloworld.py
  3. This next bit is technically optional, but it's a good habit to get into. At the top of your document add in a shebang line to specify which version of Python you'll be working with. This also enables your script to be run more like a compiled program.

    Add shebang line to Python script on Raspberry Pi
    #! /usr/bin/env python3
  4. Enter a couple of lines down. Then, use the 'print' function to tell Python to print out the phrase "Hello World!" on the screen. It looks like this:

    print("Hello World!")
    Hello World Python script on Raspberry Pi
  5. 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!

    Save Python script on Raspberry Pi
  6. You will now be back in the terminal. You'll need to make your script executable next. To do so, modify its permissions with the command below.

    chmod +x helloworld.py
  7. To run our new script, we use the command below:

    ./helloworld.py

    This will print "Hello World!" and then close the script, leaving us with the terminal to use again.

    Python run hello world script on Raspberry Pi

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.

  1. Open your script back up with Nano.

    nano helloworld.py
  2. Add a second call to 'print,' and break up "Hello World!" into "Hello" and "World!" Give each its own call to 'print.'

    print("Hello")
    print("World!")
    Python script hello world with two print lines
  3. Save and exit your script with Ctrl+X and answering Y when asked.

  4. Run your script again from the terminal. This time, you'll see "Hello" and "World!" on separate lines.

    ./helloworld.py
    Run Python script with two print lines on Raspberry Pi
  5. You're probably thinking that handling it that way is awkward and inefficient, and you'd be right. Python can take advantage of a special character to create new lines anywhere in your text. So, open your script back up to make it more efficient.

  6. Delete the second 'print' line, and add "World!" back to the first line.

    print("Hello World!")
  7. Now, remove the space between "Hello" and "World!" Replace the space with the Unix newline character, "\n"

    print("Hello\nWorld!")
    Python script with newline on Raspberry Pi
  8. Save and exit once again.

  9. Try running your script now. The result will be identical to the two separate 'print' lines.

    ./helloworld.py
    Run Python script with newline on Raspberry Pi

Hello World, Goodbye World

Using what we learned in the previous example, let's change things so we say "Hello World!" nine times. Then, as the script's about to finish, it'll print out "Goodbye World!"

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

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 '10,' 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 the count variable to determine when the loop stops.
  • If statements: Within our while loop we have one 'if' statement. It checks if the count variable is one. If it is, the script's about to end, so it'll print "Goodbye World!" instead of the usual "Hello World!" Indentation is important in Python for functions like this.
  1. Make a new file called hellogoodbye.py, and open it in nano. Create your shebang line, just like before.

    #! /usr/bin/env python3
  2. Next, import the 'time' module. Modules are pre-built chunks of code that you can bring in to your Python scripts. At more advanced stages, you'll make your own for your Python programs, but Python also contains plenty that are built in to the language itself. The 'time' module is a great example. Import it using a simple 'import' statement toward the top of your file.

    import time
  3. Now, you're going to need to set your 'count' variable. Variables in Python, and other programming languages, store information that can be changed and accessed later.

    The variable name acts as a stand-in for the info that it contains. Variables provide a consistent way to access information because the info can change at any time, but the name cannot. You'll see that in work in this script.

    count = 10
  4. A 'while' look with repeat the same code over and over as long as the condition given to it remains true. For this example, the loop will continue as long as 'count' holds a value greater than '0.'

    while count > 0:
  5. The following few lines will be the code that the 'while' loop repeats. Indentation is very important in Python. It tells Python which code belongs to which loop, statement, or function. You need to keep your indentation consistent throughout. So, for this 'while' loop, begin by indenting each line within the loop with four spaces.

    Then, provide the loop with the first bit of code to run. In this case, it's an 'if' statement. 'if' statements check if a condition is true. In the case that it is, a certain block of code will run. In this example, the 'if' statement is going to check if 'count' is equal to '1.' In Python, a singe equal sign (=) is used to assign a value. Two equal signs (==) checks if a value equals another.

    if count == 1:
  6. Just like with the 'while' loop, you're going to need to indent within the 'if'' statement too. So, indent four more spaces for everything within the 'if' statement.

    Tell Python to print out "Goodbye World!" if 'count' is equal to '1.'

    print("Goodbye World!")
  7. You can have an 'if' statement that only runs if a condition is true, and moves on, or you can tell it to do something else in the case the condition isn't met. You do this with 'else.' So, if the condition is true, do this. Else, do this other thing. So, tell your script that if 'count' doesn't equal '1,' print out "Hello World!"

    else:
    print("Hello World!")
  8. You're done with the 'if' statement, so this next part will be indented with only four spaces. You're going to use the 'sleep' function from the 'time' module that you imported to tell Python to wait a second. This isn't strictly necessary, but if you don't Python will run through the scrip too quickly to really see what's going on.

    time.sleep(1)
  9. Finally, you're going to need to tell Python to decrease the value of the 'count' variable. This way, the next time 'while' runs through, 'count' will hold a lesser value and eventually reach zero and stop. If you don't decrease the value, it'll stay at ten. The script will never say "Goodbye World!" and will loop forever.

    count = count -1

    That's kind of awkward, so there's a short-form way to handle that.

    count-=1
  10. The whole thing will look like this:

    #! /usr/bin/env python3

    import time
    count = 10

    while count > 0:
    if count == 1:
    print("Goodbye World!")
    else:
    print("Hello World!")
    time.sleep(1)
    count-=1
    Python script with loops and statements on Raspberry Pi
  11. Save and exit with Ctrl+X, Y, and Enter.

  12. Then, make your new file executable.

    chmod +x hellogoodbye.py
  13. Finally, run through, and see how it works. You'll see it print out "Hello World!" nine times, and "Goodbye World!" on the tenth and final pass.

    Run Python script with loops and statements on Raspberry Pi

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!