In this article we're going to look at how you create a program on a Raspberry Pi. We're going to look at the following:
- What is a Program?
- What's going on inside a Raspberry Pi when you run a program
- Why programming with Python rocks
- Program with Python in the Raspberry Pi terminal
- Program with Python using IDE/IDLE
Before you learn programming with a Raspberry Pi and Python, let’s look at what a program actually is. The dictionary definition is a “set of instructions that makes a computer do a particular thing.”
A computer program is a lot like a recipe. It has a list of ingredients, called ‘variables’, and a list of instructions, known as ‘statements’ or ‘functions’. You follow the instructions from the recipe one line at a time and end up with a tasty cake - and that’s no lie.
This article is based on Beginner’s Guide to Coding in issue 53 of The MagPi. Issue 54 of The MagPi has a companion piece on learning object orientated programming with a Raspberry Pi
- Click here to read Beginner’s Guide to Coding in issue 53
- Click here to read Object Orientated Programming in issue 54
The real miracle of computers is that they can do the same thing repeatedly. So you can get a machine to bake a thousand cakes without ever getting tired. A program may contain loops that make it do the same thing over and over again.
Programs also make decisions, and different paths through a program can be taken. Your recipe could make a scrummy chocolate cake or a delightful batch of doughnuts, depending on the variables (the ingredients) it has.
One thing that may surprise you when you begin programming is just how little you need to know to get started. With just a few variables, a smattering of flow, and some functions, you can get a computer doing all the hard work for you.
Program a Raspberry Pi: Inside your computer
At the heart of your Raspberry Pi are billions of voltage switches known as binary digits (or ‘bits’ for short). There are 8,589,934,592 of them in its 1GB of RAM, to be exact.
All these switches can be set to high or low, which is typically represented as 0 (for low or off) and 1 (for high or on). Everything you see on the screen, hear from the speakers, and type on the keyboard is billions of switches being turned on and off.
Obviously, it’s not that easy for humans to talk directly to computers. It’s possible to use machine language and send binary instructions directly to a computer, but this isn’t where any sane person starts (or ends if they want to remain sane).
Instead, we use a coding language to program. This is written using easy-to-understand functions like print(). These are then interpreted into machine language, which the computer understands.
We’re going to use Python to learn to code. Python is a truly great programming language. It has a rich syntax that’s free from clutter; you don’t have to worry about things like curly braces and static typing that crop up in more complicated languages like Java.
With Python, you can just create code, run it, and get things done. Python is one of the languages found most commonly inside The MagPi, so learning it here will help you understand lots of the code used in projects.
Programming with Python rocks and here's why
There are a lot of programming languages out there, and they all offer something special. Python is a great option for beginners. Its syntax (the use of words and symbols) is easy to read. And it scales all the way up to industrial, medical, and scientific purposes, so it’s ideal for beginners and experts alike.
There are two versions of Python: Python 2 and Python 3.
Python 3 is the future, so we’re going with it. Lots of courses still teach Python 2, and it’s not a bad idea to take a closer look at the differences between Python 2 and Python 3.
Python is an ‘interpreted language’. You write the code and then run the program. Under the hood, it's being translated and runs on the fly. Some languages, such as C and Java, are compiled. You write the program, then compile it to get a build file (written in machine code), then you run the build. It's a faff you can do without for now.
Program Python with Terminal
You don’t need to do anything to set up Python on your Raspberry Pi. Open a terminal in Raspbian and enter python --version. It will display ‘Python 2.7.9’. Enter python3 --version and you’ll see ‘Python 3.4.2’.
We use Python 3 in our programming guides. You can open Python 3 in the terminal by just typing python3.
The ‘$’ command-line prompt will be replaced with ‘>>>’. Here you can enter Python commands directly, just as you would terminal commands.
It’s tradition to christen any new language by displaying ‘Hello World’. Enter
And press RETURN. You’ll see ‘Hello World’ outputted on the line below.
Using the Shell is known as Interactive Mode. You can interact directly with the code. It’s handy for doing maths; enter:
1920 * 1080
And you'll get the answer: 2073600.
Mostly, you create Python programs using a regular text editor and save the files with a ‘.py’ extension. Exit Python in the Command Line using
Don’t use a word processor like LibreOffice Writer to create files – it’ll add formatting and mess up the code.
Use a plain text editor like Leafpad (Menu > Accessories > Text Editor). Here you can enter your code, save it as a program, and then run the file in the terminal.
Create a one line program with the print("Hello World") line of code and save it as hello_world.py.
Run it like this:
at the command line to run the program. It will run, display Hello World and exit.
Program Python with the IDLE IDE
You don’t have to write Python programs using a text editor like Leafpad and run them in the terminal. Instead, you can use a neat all-in-one solution, known as an ‘IDE’ (integrated development environment).
IDEs combine a text editor with program-running functionality. Often, they’ll include fancy features like debugging and text completion.
Click Menu > Programming > Python 3 (IDLE), and you’ll get a new window called ‘Python 3.4.2 Shell:’. This Shell works just like Python on the command line. Enter print("Hello World") to see the message.
You can also create programs in a built-in file editor. Choose File > New File. Enter this program in the window marked ‘Untitled’:
word1 = "Hello " word2 = "World" print(word1 + word2)
Don’t forget to include the space after ‘Hello’. Choose File > Save As and save it as hello_world2.py. Now press F5 on your keyboard to run the program. (Or choose Run > Run Module). It’ll also display ‘Hello World’ in the Shell.
The advantage of using Python IDLE is that you can inspect the program in the Shell. Enter word1, and you’ll see ‘Hello ’. Enter word2 and you’ll see ‘World’. This ability to inspect and use the variables in your program makes it a lot easier to experiment with programming and detect bugs (problems in your code).
Subscribe and never miss an issue
Get a a brand new Raspberry Pi Zero W, a case for it, and a selection of adapter cables with a twelve-month print subscription to The MagPi!