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.
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?
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)”.
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
The terminal icon
When Python 3 loads, the visible window is the shell, which has two main purposes:
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.
Creating a new file
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))
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.
The executed program with user data filled in
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!