“
backwards-compatible.” In other words, some of
the changes in Python 3 caused
programs written in earlier versions of
Python to produce syntax errors.
As a simple example, if we
wanted to display “Hello World!” on
the RPi monitor (which we will
shortly do), in Python 3 we would
write print (“Hello World!”); in all
earlier versions of Python, we would
write print “Hello World!”
Understandably, many Python
programmers were somewhat upset
by this situation. However, others
welcomed the changes because they
make Python more consistent and
more readable. (Search for “Python 2
vs. Python 3” if you’re interested in
the details of the debate.)
Version 2
October 2013 13
SHARPENING YOUR TOOLS OF CREATIVITY
for index = 1 to 20
if index // 4 = 0 then
high LED
else
low LED
endif
wait 1
next index
Both code snippets implement
the same task — which could also be
accomplished with simpler code —
but that’s not the point. Rather, the
question is which snippet is easier to
read and understand? (If you chose
Version 2, you’re absolutely correct!)
If you’re a regular Primer reader,
you know that I often encourage
readers to email me with any
PICAXE-related questions or
problems they may be having. I
frequently receive software questions
accompanied by a program listing.
Of course, we have to make a
choice at this point: Are we going to
use Python 2 or Python 3 for our
Sometimes (fortunately not often) the
listings look a lot like Version 1
above. Before I can even begin to try
to figure out what’s going on, I have
to edit the program so that it’s
properly formatted and more easily
readable.
PICAXE-Pi experiments? I have to
admit, I’m having a little trouble
deciding!
The point of all this is that I’ve
switched back to using Python 3. 2.
Naturally, I can’t rule out the
possibility that I won’t run into
additional problems with Python 3.
Fortunately, however, Python 2. 7 and
Python 3. 2 are both pre-installed in
the latest version of the RPi’s
operating system, so switching
between the two versions won’t be a
problem.
Experimenting With
Python 3. 2
With Python programs, I won’t
need to do that because Python
forces us to correctly format all our
programs. That’s why I love this
feature! (There’s another major
benefit to formatting a Python
program; we’ll discuss
that one when we get
to our “Hello World!”
stuff.)
When I first started
experimenting with Python, I chose
to use version 3. 2 because I knew
the Python community was already
moving in that direction, and I didn’t
want to end up trying to learn both
versions, probably getting confused
in the process. At that point, I
intended to use I2C to communicate
between the PICAXE and RPi, but I
quickly discovered that an I2C library
was not yet available in Python 3. 2,
so I switched to using Python 2. 7.
After experimenting with I2C for
a few days, I realized that it’s fairly
complicated to implement reliable
two-way communications between
the two processors. It turns out that
good old-fashioned serial
communication looks like a better
choice. (We’ll get into those details in
a future column.)
Python 2 vs.
Python 3
Python has been
around for almost 20
years, and it has
evolved into a very
popular and easy to
use language. However,
when Python 3.0 was
introduced in 2008, it
created quite a stir in
the Python community
because it was the first
version that wasn’t
■ FIGURE 1. The IDLE3 Python shell.
When we program in PICAXE
BASIC, we use either the
Programming Editor or AXEpad
software — both of which are PICAXE
IDEs (Integrated Development
Environments). Similarly, there are
several IDEs for Python programming.
In the Python world, an IDE is
usually referred to as a
Python shell, and the
shell that’s included in
the RPi operating
system is called IDLE.
Actually, when you
look at the RPi
desktop, you will see
two different IDLE
icons. IDLE is used for
Python 2.x
programming, and
IDLE3 is used for
Python 3 programming.
On your RPi,
double-click the IDLE3
icon and you will see a
window that’s similar to
the one shown in
Figure 1 which is a
screenshot from my