Welcome to your first tutorial on learning Python on the Raspberry Pi. We’ll begin our journey in the same manner as learning any programming language by getting our RPi to say hello to the world.
There are two versions of Python installed on the Raspberry Pi: version 2 and version 3. For our tutorials we will be using version 3. To start we want to open up IDLE: the Python environment.
Note that IDLE is the Python 2 environment and IDLE 3 is for Python 3.
You will then be presented with the Python Shell:
Here we can type Python commands and view the results. Start by typing: print(“Hello World”), then hit Enter.
Congratulations you have just written your first Python command. If all goes well then it should output Hello World.
We can also try a little bit of maths, type in 3+2 and hit Enter. The result will be displayed below.
As you can see Python is an incredibly simple language. Unlike other programming languages, a semicolon is not required at the end of a line to indicate the end of a command. This will become clearer as we build larger programs.
Interpreted Not Compiled
There are many different types of programming languages, the most common being Procedural languages and Object Orientated languages and these can both be further categorised into Interpreted languages and Compiled languages.
An object orientated language (such as Java or C++) is made up of classes and functions and the start of the program is detonated by certain keywords (such as having a function called main). Languages such as Python or C are procedural languages. This means that when it is run, the script is opened at the top and the interpreter or compiler will work its way down processing each command.
Programs such as C or Java have to be compiled before they are run. After the developer is done writing their code, a compiler will run through it and produce output files that can be distributed. Very large programs may require some time to compile them, but once complete the program will run much faster. Interpreted languages work in the same way but instead of being compiled during the development, they are “compiled” every time they are run.
Usually interpreted languages are designed to be simple and easy to learn. Very complex programs would not be interpreted procedural code (like Python) because it would not be able to take full advantage of the computer it is running on. It is for this reason that we start by learning a simple language like Python and then proceed to more complicated languages such as C++ (which is an object orientated compiled language).
Whilst we’ve had a quick look at typing commands in to the console, this isn’t useful for coding large programs. Instead we can write all our commands in one file and then run that file. Before we start, create a folder in your documents to save all of your scripts.
We can then go back to IDLE and select New Window from the File menu.
We are presented with a blank text editor that we can type into. Start by typing in
Then select Save from the File menu:
Navigate to your Python folder and give it a name – Python files need to be saved with the extension .py.
Once your script is saved, you can then select Run Module from the Run menu.
Your script will then be shown in the Python Shell:
Congratulations, you have just created your first Python programs. If you have any questions feel free to post in the comments below.