To begin programming using Python you will need to think like a problem solver and in time you will naturally think like a programmer. As you build up your knowledge of programming you will be able to apply your programming knowledge to solve these issues.
There are many languages that each has been designed to solve a purpose. For example, there is a language designed for real time modelling and others for high speed database transactions. These types of languages can fall into two distinct groups, high level languages and low level languages. Python is an example of a high level language because it is removing you from the burden of understanding the Raspberry Pi hardware and thus taking you further away from the actual device. High level languages are easier to write, tend to be shorter and are portable. This means that you can write a Python program on your Raspberry Pi and it will work on a Windows 8 computer (Providing Python has been installed). They are also easier to debug and locate errors.
On the other end of the scale exists low level programming. These are more complex to program but have a speed advantage and take you closer to the hardware. By bringing you closer to the hardware you will be required to have a better understanding of how the hardware functions and the computer memory stores its variables. This can be an advantage or a disadvantage depending on your view.
Low level programs can only run on the same type of computer device. For example, if you write a program using a low level programming language on the Raspberry Pi, it will not work on a Windows 8 computer.
Even though programs are written in a high level language they will eventually into a low level language. For this process to happen either an interpreter is used or a compiler. The interpreter reads a program line by line and runs it. A compiler will read the program that you have written, which is known as source code and translates this it into something called object code or the executable program. The executable program is the final result of your program which runs the program. You do not need to compile the program every time in order to run it but if you make any changes to your program then you will need to re-compile the program for the changes to take effect.
As Python will be the preferred choice of language, we need to check that it is installed.
$ python -V
If Python has been installed then it will display the version number that is installed. These examples will use Python 3 and above so make sure you have version 3 or above installed. Some distributions already have Python installed for the Raspberry Pi but in case this isn’t, you can always enter the following command.
$ sudo apt-get install python3
Python 3 comes with IDLE which should be on your Raspberry Pi desktop. Start IDLE by double clicking on the icon.
When the IDLE interface appears just press enter.
The arrows above are prompting you to enter commands into IDLE
Enter the following lines below.
>>> johnsage = 18
>>> janesage = 21
Enter the following to display the value contained in johnsage.
>>> print (johnsage)
The print command will output the value contained within this variable.
>>> print (janesage)
You can also combine variables to obtain a result
Other mathematical operations can be performed in IDLE.
Enter the following examples below.
>>> print (janesage-johnsage)
You can also create a new variable based on the result of performing actions on existing variables. For example
>>> combined_age = johnsage + janesage
>>> print (combined_age)
IDLE can also produce the results without the need to store numbers in a variable. For example enter.
You can also multiply by using the * symbol
To use division simply apply the forward slash / symbol.
You can also do the same with text strings except text strings except we need to wrap text within double quotes. For example,
>>> “Python ”+”Programming”
Notice that in IDLE you do not have to use the print statement to output the result but it is good practice to use it because it will make future programming easier when you start using Python files.
>>> print(“Pyhon programming for the Raspberry Pi”)
Python programming for the Raspberry Pi
To use comments in Python just enter the # (hash/pound) symbol.
>>> #this is a comment
Notice that IDLE does not display any of the text that appears after the # symbol.
Python will be revisited later but for now this should give you a very basic idea of what to expect from Python.
If you prefer to jump ahead a learn Python and PyGame programming the books below are a great place to start.
Next Raspberry Pi Beginning C Programming Week 7 – Day 3 >>