Chapter 1
9
Why Python?
It is often asked, "Why has Python been selected as the language to use on the Raspberry
Pi?". The fact is that Python is just one of the many programming languages that can be used
on the Raspberry Pi.
There are many programming languages that you can choose, from high-level graphical block
programming, such as Scratch, to traditional C, right down to BASIC, and even raw Machine
Code Assembler. A good programmer often has to be code multilingual to be able to play
to the strengths and weaknesses of each language in order to best meet the needs of their
desired application. It is useful to understand how different languages (and programming
techniques) try to overcome the challenge of converting "what you want" into "what you get"
as this is what you are trying to do as well while you program.
Python has been selected as a good place to start when learning about programming by
providing a rich set of coding tools while still allowing simple programs to be written without
fuss. This allows beginners to gradually be introduced to the concepts and methods on which
modern programming languages are based without requiring them to know it all from the
start. It is very modular with lots of additional libraries that can be imported to quickly extend
the functionality. You will nd that over time, this encourages you to do the same, and you will
want to create your own modules that you can plug in to your own programs, thus taking your
rst steps into structured programming.
Like all programming languages, Python isn't perfect; things such as adding a space at the
start of a line will often break your code (indents matter a lot in Python; they dene how
blocks of code are grouped together). Generally, Python is slow; since it is interpreted, it
takes time to create a module while it is running the program. This can be a problem if you
need to respond to time critical events. However, you can precompile Python or use modules
written in other languages to overcome this. It hides the detail; this is both an advantage and
disadvantage. It is excellent for beginners but can be difcult when you have to second-guess
aspects such as data-types, but this in turn forces you to consider all the possibilities, which
can be a good thing.
Python 2 and Python 3
A massive source of confusion for beginners is that there are two versions of Python on the
Raspberry Pi (Version 2.7 and Version 3.2), which are not compatible with one another, so
code written for Python 2.7 may not run with Python 3.2 (and vice versa).
The Python Software Foundation is continuously working to improve and move forward
with the language, which sometimes means they have to sacrice backward compatibility in
order to embrace new improvements (and importantly, remove redundant and legacy ways
of doing things).