Page 31 - Programming the Raspberry Pi Getting Started with Python
P. 31
3
Python Basics
The time has come to start creating some of our own programs for the Raspberry Pi. The language we
are going to use is called Python. It has the great benefit that it is easy to learn while at the same time
being powerful enough to create some interesting programs, including some simple games and
programs that use graphics.
As with most things in life, it is necessary to learn to walk before you can run, and so we will begin
with the basics of the Python language.
Okay, so a programming language is a language for writing computer programs in. But why do we
have to use a special language anyway? Why couldn’t we just use a human language? How does the
computer use the things that we write in this language?
The reason why we don’t use English or some other human language is that human languages are
vague and ambiguous. Computer languages use English words and symbols, but in a very structured
way.
IDLE
The best way to learn a new language is to begin using it right away. So let’s start up the program we
are going to use to help us write Python. This program is called IDLE, and you will find it in the
programming section of your start menu. In fact, you will find more than one entry for IDLE. Select
the one labelled “IDLE 3” after it. Figure 3-1 shows IDLE and the Python Shell.
Figure 3-1 IDLE and the Python Shell
Python Versions
Python 3 was a major change over Python 2. This book is based on Python 3.1, but as you get further
into Python you may find that some of the modules you want to use are not available for Python 3.
Python Shell
What you see in Figure 3-1 is the Python Shell. This is the window where you type Python commands
and see what they do. It is very useful for little experiments, especially while you’re learning Python.
Rather like at the command prompt, you can type in commands after the prompt (in this case, >>>)
and the Python console will show you what it has done on the line below.
Arithmetic is something that comes naturally to all programming languages, and Python is no
exception. Therefore, type 2 + 2 after the prompt in the Python Shell and you should see the result
(4) on the line below, as shown in Figure 3-2.