Languages,  Microcontrollers,  python,  raspberry pi

Starting with Python on Raspberry Pi

I’ve programmed in many languages over the years. So many, in fact, that I’d be hard pressed to name them all, and unlikely to succeed. One of the things that I’ve learned in the process is that most of them are easy to pick up: just find a small working program or three for reference, a reference manual, and start writing. As you start understanding the very basics, only then is it time to look at rules, tutorials, and such (and I recognize that other people will very well learn in a different manner).

As I’m playing with Pis, I’m picking up python for no real reason other than that is what is usually used to program them, and almost all of the available examples seem to use it. As I do so, I’m including some basics here.

On thing to note that is different from almost all the other languages I’ve used in the past is that white space matters. The only other example I can think of that I’ve used is the first six columns in older FORTRAN–and even then, it’s not white space mattering, but rather that the first six columns of a line have meaning. In Python, adding another space or more of indentation sets levels of code, in a way vaguely analogous to the braces in C.

The first line

Python is an interpreter, and programs can be launched from the command line, other programs, etc. (it also has an interactive mode, but I have no interest in it unless perhaps I someday test a function). As such, the first line specifies the location of the interpreter to use. On Raspbian, this is

#!/usr/bin/python

So a very simple program would be

#!/usr/bin/python
print "***splat***"

While you could start the program by invoking python, it is easier (at least in the long run) to make the file executable. So type

chmod u+x testpy.py

and then check your results with

ls -l testpy.py

and you should get something like

-rwxr--r-- 1 pi pi 40 Dec 26 20:25 testpy.py

Of which the “x” in the first cluster tells you that it is executable for the owner (pi), and the lack of “x” in the two later clusters tells you that others cannot execute it (but they could read it and make their own copy to execute).

So give it a run with

./testpy.py

and you should see something like

pi@sprinklers:~ $ ./testpy.py 
***splat***
pi@sprinklers:~ $ 

Congratulations; you’ve successfully run a python program on your raspberry. Everything else is just a matter of scale . . .

(and now I’ll take a minute to scratch my head and wonder why this much is considered a tutorial in and of itself by so many people . . . I guess it has something to do with advertising . . .)

Since a basic first session, should do *something*, let’s try a loop.

We’ll change testpy.py to have a loop rather than a splat:

#!/usr/bin/python
for myVal in (3,7,9):
    print "at ", myVal
print "***splat***"


This shows a couple of things at the same time.

First, python doesn’t use a defined iteration like most languages I’ve used. Instead, you provide a sequence for it to use. I specified a set of numbers, while i could also have used “range(1,5)”, for example, to produce the sequence of numbers from 1 to 5. Many other options exist, such as strings

Second, notice the whitespace based scope. This is indented by four spaces, the default for python in vim. Any number of spaces could have been used–and as long as things stay at that indentation level, they would be part of the loop. Once indentation returned to the level of the “for”, the loop is ended. Thus the first print is repeated, while the last is not:

pi@sprinklers:~ $ ./testpy.py 
at  3
at  7
at  9
***splat***
pi@sprinklers:~ $ 

Third, there Python does not require (or even, apparently, allow!) declaration of variables.

Fourth, the use of the “:” separator at the beginning of a control structure such as “for”.

Let’s take a look at “while” before wrapping this up. Thinking ahead to the Pi’s intended role as a monitor, it will be looping forever. As we don’t want this post to go on forever, we’ll exit early:

#!/usr/bin/python

myVal= 1

while True :

    print "at ", myVal
    myVal+=1
    if myVal >= 4 :
        break

print "***splat***"

So that snuck in a few details.

We see multiple lines at the same level during the while.

We see the “+=” operator to add to a variable (and there is a rich family such operators).

The “if” is nested at the same level as the other loop commands, and needs the same colon to operate.

The “break” command, indented so as to be part of the if, and which breaks out of the loop.

Leave a Reply

Your email address will not be published. Required fields are marked *