/ python

Learning Python for the Raspberry Pi

Once you’ve connected the peripherals, installed NOOBS, and gotten your Raspberry Pi up and running, pat yourself on the back and take a moment to bask in the glow of the large raspberry on your Raspbian desktop.

raspberry pi default wallpaper

Whee. Okay, that’s enough gloating, ya narcissist. So now what?

Choosing a Language

After getting my first Raspberry Pi 2 up and running last weekend, and taking some cursory glances at the sample apps, I wanted to get into some actual development. After all, if we can’t tell it to do anything, then it’s not much more than a $35 paper weight with a network jack.

But first, what language(s) does the Raspberry Pi support?

The Raspberry Pi Foundation recommends Python as a language for learners. We also recommend Scratch for younger kids. Any language which will compile for ARMv6 (Pi 1) or ARMv7 (Pi 2) can be used with the Raspberry Pi, though, so you are not limited to using Python. C, C++, Java, Scratch, and Ruby all come installed by default on the Raspberry Pi.

Is there a list somewhere that says which languages “will compile for ARMv7”? I couldn’t find one. After a little research though, it seems the Pi is capable of supporting many other languages, such as Perl, Erlang, PHP, Go, etc. The foundation’s seeming preference for Python is just because they believe it to be easy for newbies to pick up.

If you’re very familiar with Java, there’s a couple IDEs installed with Raspbian. Or maybe you want to play with a new(er) language like Go (not pre-installed). Or something with a strong community like Ruby.

Since most of the beginner examples I’ve seen so far are in Python, that’s what I’ll be using first.

Pick a Python… any Python

Under the Menu, in the Programming subfolder, you should see two “Python” options… one for the 2.x version and another for 3.x.

app-menu-python3

If you’re bored and want to read all about why there are two versions available, check out “Should I use Python 2 or Python 3 for my development activity?” The tl;dr version is that Python 3 is newer and better, in part because it chucked out some baggage from Python 2. Since 3.x isn’t fully backwards-compatible, old Python 2 apps (and old Python 2 third-party packages) will not necessarily work with Python 3.

That’s a concern if you’re supporting an old Python app, but not if you’re just playing around, so choose Python 3.

I actually don’t feel like trying to learn a new language on the Raspberry Pi itself, so I’ll be downloading the Python release for my laptop and using it there, then copying programs to the Pi to try them out and tweak them. Haven’t figured out the best way to do that yet.

Python Development

We’ve got a few options for development.

The Python install comes with an app called IDLE that allows you to hit the ground running. Fire it up and try out some basic commands.

python 3 idle

It’s pretty awkward trying to develop anything of substance like that though.

You can create a file, place a few lines of text in it, and then call it from the command line with:

python3 /path/to/your/file/justatest.py

Note that I called it using “python3”. Typing “python” uses Python 2, whereas “python3” uses Python 3. Below, I’ve created a file with a single line of code, and run it using both commands. The printed value underscores one of the differences between 2.x and 3.x. The division operator performs integer division in 2.x (drops the fractional part) when you use two integers, but in 3.x it keeps the fractional part.

python vs python3

Once you really get going, you can check other IDEs that support Python development. A good IDE will provide helpful syntax suggestions and other tools to make development easier. I decided to try PyCharm from JetBrains, because they have a free community version and I’ve used some of their software (such as ReSharper) and it’s always been solid.

pycharm

There are also some online Python editors that let you program without the need to install anything. I haven’t tried it, but PythonAnywhere has a free plan, so that’s another option for you too.

Learning the Basics (and Beyond…)

The Pi website has its own Python Documentation, to get developers started (the main website is seriously packed with good info).

I also found a series of free courses from the University of Michigan (hosted on Coursera) that teach different aspects of Python development. I’ve enrolled in them myself.

Here’s an interesting series of videos called My First Raspberry Pi Game that uses Python. Might be worth a look too.

And if you’re thinking of starting with Scratch instead, or with introducing kids to the Pi, there’s a course on edX called Programming in Scratch that may be for you.

Good luck! And let me know how it goes!


Grant Winney

Grant Winney

I write when I've got something to share - a personal project, a solution to a difficult problem, or just an idea. We learn by doing and sharing. We've all got something to contribute.

Read More