The Raspberry Pi is a fantastic single-board computer for making hardware projects, and in a previous article, we looked at how Python can be used with the Pi. In this article we will learn some Python basics and learn how to make a basic practical program.

Python as a Language

You can program computers in many different languages such as C, C++, C#, Java, Python, etc., but most modern languages operate in similar ways. Most are object oriented, use somewhat similar syntax, and if you know one well, then you can definitely migrate to other languages easily.

Python as a language tries to provide the programmer with simplicity and code readability while also providing complex functions including multi-threading and networking. Unlike compiled languages such as C and C++, Python is a byte-code language, which means several things. First, a computer can only run Python code if it has a Python interpreter, but this is in fact an advantage, as it makes the code near platform independent (besides a few specialized routines). Second, because it is either interpreted or compiled into Python byte code, it is considerably slower than compiled languages, and so is not suitable for game engines, complex simulations, or large data processing. However, it is a very useful language for lightweight applications, and is especially useful on the Raspberry Pi, as it can interact with the GPIO (general purpose input output). This means that it could be used to control motors, LEDs, sensors, displays, and even a floppy disk if you wanted!

Python Basics

When teaching code, it can be difficult to decide where to start, because even the most basic program consists of multiple concepts that work together and interact at the same time. So to start, we will look at variables, what they are, and why you would use them.

Variables

A variable in a program can be thought of as a box that can be used to store one thing at a time. While only one item can be put in this box at a time, the item could be anything, such as a number, a sentence, or even a picture. But remember, the box can only hold ONE thing at a time. What makes variables useful is that not only can they store things, but they can be named too! To assign a value to a variable we use the equals sign, and below we can see two different types of variable use.

FirstName = “Robin”



Age = 24

The first variable is called “FirstName”, and we store the word “Robin” into it, while the second variable is called “Age” and we store the number “24”. You may have noticed the quotation marks around the word “Robin”, and this is because Python, like many languages, uses quotation marks to indicate sentences and words. If there were no quotation marks, Python would try to interpret what Robin means, and since there is no definition of Robin, it would produce an error.

A few notes on variables:



They should start with a capital letter, but this is not important (only a best practices thing)



They MUST start with a letter



They must not contain spaces or symbols except “_”



No two variables can have the same name



Variables can be operated on in many different ways. For instance, we can do all the basic mathematical functions, including addition, subtraction, division, and multiplication. Some operators (such as addition) can also be applied to strings (words and sentences in Python are called strings) as we can see below.

A = 10



B = 20



C = A + B



FirstName = “Robin”



LastName = “Mitchell”



FullName = FirstName + “ “ + LastName

The following is a table of some basic operators and what they do.