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!
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.
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.
But what good is a program if the only thing we can do is assign variables values and then do basic mathematical operators on them? All we have so far is a trivial calculator which lacks a user-friendly way of getting values. How do we display our variables once processed? This is where two functions come into play: print and input.
Let’s say we have performed some calculations on a variable and we want to see what the value is after. This is very easy to do in Python with the instruction “print()”! Before we continue, we need to understand what a function is. A function can be thought of as a machine that takes stuff in, operates on it, and performs an action as a result. In the case of “print()”, the function takes in a variable, string, or both, and prints out their values. But print can also be used to print a string or number that is not a variable, as we will see below. But remember that since they are strings, they need quotation marks!
print(“Hello”) → Prints the word “Hello”
print(FirstName) → Prints the word “Robin”
print(A) → Prints the value of A
print(A + B) → Prints the value of A + B
Still, even though we can now see our variable values, changing variables requires editing the program code, which is not ideal, especially in applications like robots, which receive data autonomously. One way to obtain variable values from the user is to use the function input(). When called, the user will be prompted to enter a value, whether it be a string or number. When the user has entered a value (by pressing enter), the input() function returns a value, which should be placed into a variable.
FirstName = input() Print(FirstName) The input function can also print text so that the prompt will be on the same line as the user’s input instead of on a new line.
The input function can also print text so that the prompt will be on the same line as the user’s input instead of on a new line.
FirstName = input(“Enter Name ”)
The issue with input() is that it returns a string and not a number. If you want to use input to return numbers instead of strings, you need to do typecasting. Typecasting is simply a conversion between data types (whether they be numbers of strings), so if you want to get a number from the user, then use “int(input())” instead.
A = int(input(“Enter Number “))
Our First Program
Armed with our basic knowledge of Python, we will now design a simple program that will ask for a person's first name, last name, and their age. The program will then print out these details.
FirstName = input(“Enter your first name: “) LastName = input(“Enter your last name: “) Age = int( input(“Enter your age: “ ) ) print (“Your full name is “ + FirstName + “ “ + LastName) print(“Your age is “ + Age) SecondsLived = Age * 365 * 24 * 60 * 60 print(“You have lived for “ + SecondsLived + “ seconds”)