The best tools to make your project dreams come true

Login or Signup

3/8/2018 | By Maker.io Staff

How to Create a Raspberry Pi Python Program

One of the biggest selling points for Raspberry Pi is adaptability, thanks to its extensive I/O options. In this tutorial, we will learn how to get started with Python on the Raspberry Pi 3 Model B.

Things You Will Need

  • Raspberry Pi (A or B)
  • Python IDE

A Bit About Python

Python is a programming language that has recently become very popular—so popular, in fact, that it is now the fourth most popular language (according to the TIOBE index). But why has Python become so popular? What makes it an ideal language for the Raspberry Pi?

Unlike other languages (C, C++, etc.), Python is an interpreted language, which means when a Python program is executed, an interpreter reads the Python instructions and then performs the desired action. The interpreter itself is written in CPU native instructions, but the Python program is not. This means Python programs can, in theory, run on ANY computer, so long as that computer has a Python interpreter. This makes Python a cross-platform language, which is one of the main reasons why it has become so popular. A program written on Windows does not need to be rewritten to work on a Mac or a Linux machine. It’s also incredibly simple, easy to read, and powerful. It can write advanced programs, including graphical interfaces, networking, parallelism, and much more. Python is a productive language, resulting in faster program production and requiring less code and time.

Since Linux has been written for the Pi (ARM core), the Python interpreter can run on it and, therefore, so can Python programs. So, how do we start with this wonderful programming language on the Pi?

Installing and Loading the IDE

Raspbian, the default OS choice for the Raspberry Pi, should contain both Python 2 and 3, so loading Python should be easy to navigate through menu options. Firstly, click the top left Pi icon on the menu bar. Then navigate to “Programming” and click “Python 3 (IDEL)”.

Image of Loading Python 3 IDE

Loading Python 3 IDE

If you do not have Python on your system, you will need to install it. To do this, open a terminal window and enter the following command:

sudo apt-get install python3

Image of The terminal icon

The terminal icon

Understanding the IDE

When Python 3 loads, the visible window is the shell, which has two main purposes:

  1. Entering Python commands: some quick calculations
  2. Input/output for programs: getting data from a user and displaying information

Image of The main shell

The main shell

The IDE also has a menu bar with many options, including file operations, editing, shell specific options (“Interrupt program” for example), program debugging, IDE options, and “Help”. For now, we will only concern ourselves with creating a new file, which is done by clicking File > New File.

Image of Creating a new file

Creating a new file

Creating Our First Program

Once the new window loads, we can finally enter our first Python program. To do this, enter the code as shown below into the window. Then, save the file.

# A Basic Python Program

# Declare some variables first
name = ""
age = 0
currentYear = 0
yearBorn = 0

# ----------------------------------
# Start by getting the users details
name = input("Enter your name: ")
age = int(input("Enter your max age this year: "))
currentYear = int(input("What is the current year? : "))

# ----------------------------------
# Perform a basic calculation
yearBorn = currentYear - age

# ----------------------------------
# Print the result
print("You were born in the year " + str(yearBorn))

Running Our Program

With the code entered and the file saved, it’s time to run the program. Running a Python program can be done in one of three ways: press F5 in the window with the program to run, go to the menu bar and click Run > Python Shell, or run the file via a terminal window as an argument for Python. For now, the easiest way is to simply press F5 in the window with the code. Once pressed, the code should return no errors, and the shell window should prompt for data.

Image of The executed program with user data filled in

The executed program with user data filled in

Conclusion

Getting Python programs to run on the Raspberry Pi is incredibly easy with an OS like Raspbian, which is designed for programmers of all kinds. Another major advantage of using Python on the Pi is that you can write the program on a different computer (such as Windows) and then transfer the program to the Pi when it’s ready to run. Just remember, however, that some code in Python is unique to the Pi—only the Pi has GPIO!