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 tempting to jump straight into the projects that attracted you to the device. Robots, sensors, music players, and similar projects are great uses for the Raspberry Pi. However, these projects aren't the best start for someone new to the device. Instead, learn the basics before charging into a complex project.

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

A Gentle Introduction

A common first project 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. This project gives you an easy and relevant introduction to Python. It's also a function you'll frequently use in future projects.

Here are a few variations of this traditional lesson to kick off your programming skills with the Raspberry Pi. We'll use Python scripts instead of IDLE.

Hello World

You'll start at the beginning with the basic printing of the text Hello World.

  1. Log in to a terminal session, then enter touch helloworld.py to create a new python script called helloworld.py.

    The Linux touch command creates a new blank document. The document name is helloworld and has the .py extension to mark it as a Python script file.

    Create Python script Raspberry Pi
  2. Enter nano helloworld.py to open the new file with Nano.

    Nano is the text editor used in this example. It's a simple editor that comes with most Linux systems.

    Open Python file with Nano on Raspberry Pi
  3. This next bit is technically optional, but it's a good habit to get into. At the top of the document, add in a shebang line to specify the version of Python you're using. This also enables the script to be run like a compiled program. To add the shebang line, enter:

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

    print("Hello World!")
    
    Hello World Python script on Raspberry Pi
  5. Press Ctrl+X and then press Y to save the file. The terminal asks you to press Enter to save the file with the specific file name. You've just created your first Python file.

    Save Python script on Raspberry Pi
  6. In the terminal, make the script executable. To do so, modify its permissions with this command:

    chmod +x helloworld.py
    
  7. To run the new script, enter this command:

    ./helloworld.py
    

    This prints Hello World! and closes the script, making the terminal available to use.

    Python run hello world script on Raspberry Pi

Hello, Then World

Time to go up a gear. This example prints the word Hello on one line and World! on the next. This adds a new line to your Python file at a simple level.

  1. Open your script with Nano. Enter the following in a terminal:

    nano helloworld.py
    
  2. Add a second call to print, and break up Hello World! into Hello and World! To give each word its own call to print, enter the following:

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

  4. Run the script from the terminal. This time, you'll see Hello and World! on separate lines. Enter this command to run the script:

    ./helloworld.py
    
    Run Python script with two print lines on Raspberry Pi
  5. The script can be made more efficient. Python can take advantage of a special character to create new lines anywhere in your text. Delete the second print line, and add World! to the first line. The script should now look like this:

    print("Hello World!")
    
  6. Remove the space between Hello and World! Then, replace the space with the Unix newline character, \n. The command looks like this:

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

  8. Run the script. The result is identical to the two separate print lines. To run the script, enter:

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

Hello World, Goodbye World

Using what you learned in the previous example, you'll change things so that the script says Hello World! nine times. Then, as the script is about to finish, it prints Goodbye World!

These instructions introduce a few new concepts:

  • Import: The import line brings in the time module, which allows you to ask the file to wait for a period of time where you specify. Do this with time.sleep(1) later in the file.
  • Count: The count line defines a variable, which is a way of holding a value and giving it a name. You'll start this at 10, which is explained further down.
  • While: The while loop loops the code while a condition is true. In this case, the count variable determines when the loop stops.
  • If statements: Within the loop is one if statement. It checks if the count variable is one. If it is, the script is about to end, so it prints Goodbye World! instead of Hello World! Indentation is important in Python for functions like this.
  1. Make a new file called hellogoodbye.py, and open it in Nano. Then, create a shebang line, like this:

    #! /usr/bin/env python3
    
  2. Import the time module. Modules are pre-built chunks of code that you can add to Python scripts. At more advanced stages, you'll make your own for your Python programs. Still, Python contains several that are built into the language. The time module is a great example. Import it using a simple import statement toward the top of the file, like this:

    import time
    
  3. Set the 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 information that it contains. Variables provide a consistent way to access information because the information can change at any time, but the name cannot. You'll see that in work in this script:

    count = 10
    
  4. A while loop repeats the same code repeatedly as long as the condition given to it remains true. For this example, the loop continues as long as count holds a value greater than 0.

    while count > 0:
    
  5. The following few lines are the code that the while loop repeats. Indentation tells Python which code belongs to which loop, statement, or function. Keep indentation consistent throughout.

    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 that checks if a condition is true. If it is, a certain block of code runs.

    In this example, the if statement checks if count is equal to 1. In Python, a single 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, indent within the if' statement. So, indent four more spaces for everything within the if statement.

    To tell Python to print 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. If the condition is true, do this. Otherwise, tell the script that if count doesn't equal 1, print Hello World!

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

    time.sleep(1)
    
  9. Tell Python to decrease the value of the count variable. This way, the next time while runs, count holds a lesser value and eventually reaches zero and stops. If you don't decrease the value, it stays 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 completed script looks 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 by pressing Ctrl+X, Y, and then Enter.

  12. Make the new file executable.

    chmod +x hellogoodbye.py
    
  13. Run the script to see how it works. You'll see it print Hello World! nine times, and Goodbye World! on the tenth and final pass.

    Run Python script with loops and statements on Raspberry Pi
Was this page helpful?