Programming Raspberry Pi: Getting Started with Python (1st Edition)

The book Programming the Raspberry Pi: Getting Started with Python tells you all you need to know to get you started programming you Raspberry Pi in Python. It includes chapters on graphical user interfaces with Tkinter, as well as games programming with pygame.
Three chapters of the book are devoted to using the GPIO connector and attaching electronics to the Raspberry Pi. Example projects include an LED clock and a Pi controlled robot rover.
Chapter 1. Introduction.
  • What is the Raspberry Pi?
  • What can you do with a Raspberry Pi?
  • A Tour of the Raspberry Pi
  • Setting Up
  • Booting Up
  • Summary
Chapter 2. Getting Started.
The Raspberry Pi uses Linux as its operating system. This chapter introduces Linux and shows the reader how to use the desktop and command line.
  • Linux
  • The Desktop
  • The Internet
  • Applications
  • Internet Resources
  • Summary
Chapter 3. Python Basics.
The time has come to start creating some of your own programs for the Raspberry Pi. The language that we are going to use is called Python and it has the great benefit that it is easy to learn, whilst at the same time being powerful enough to create some interesting programs, including the use of graphics and some simple games.
  • What is a Programming Language?
  • IDLE
  • Numbers
  • Variables
  • For Loops
  • Simulating Dice
  • If
  • While
  • Summary
F11_01 web
Chapter 4. Strings, Lists and Dictionaries.
This chapter could have had ‘and Functions’ added to its title but it was already long enough. In this chapter, you will first explore and play with the various ways of representing data and adding some structure to your programs in Python. You will then put it all together in the simple game Hangman.
  • String Theory
  • Lists
  • Functions
  • Hangman
  • Dictionaries
  • Tuples
  • Exceptions
  • Summary of Functions
  • Summary
Chapter 5. Modules, Classes and Methods.
In this chapter, we will explain how to use and make our own modules such as the ‘random’ module that we used in the last chapter.
We will also explain how Python implements something called object-orientation. Object-orientation allows programs to be structured into classes, each responsible for their own behavior. This helps to keep a check on the complexity of our programs and generally makes them easier to manage.
  • Modules
  • Object-Orientation
  • Defining Classes
  • Inheritance
  • Summary
Chapter 6. Files and Internet.
Python makes it easy for your programs to use files and connect to the. You can read data from files, write it to files and fetch content from the Internet, or even check for new mail or tweet all from your program.
  • Files
  • Pickling
  • Internet
  • Summary
F10-01 web
Chapter 7. Graphical User Interfaces.
Everything we have done so far has been very text-based. Our Hangman game would not have looked out of place on a 1980’s home computer. This chapter will show you how to create applications with a proper graphical user interface (GUI).
  • Tkinter
  • Hello World
  • Temperature Converter
  • Other GUI Widgets
  • Dialogs
  • Menus
  • The Canvas
  • Summary
Chapter 8. Games Programming.
This chapter will introduce you to a very handy library called Pygame, and get you started using it to build a simple game.
  • What is Pygame?
  • Hello Pygame
  • A Raspberry Game
  • Summary
Chapter 9. Interfacing Hardware.
In this chapter we explore the various ways of connecting the Pi to electronic devices using the GPIO, using some of the first products that have become available to help you do this.
  • GPIO Pin Connections
  • Direct connection to GPIO Pins
  • Expansion Boards
  • Prototyping Boards
  • Arduino and Pi
  • Summary
Chapter 10. Prototyping Project (Clock).
In this chapter, we will build, what can only be seen as a grossly over-engineered LED digital clock. We will be using a Raspberry Pi, Adafruit’s Cobbler lead some breadboard and a 4 digit LED display.
  • What You Need
  • Hardware Assembly
  • Software
  • Phase Two
  • Summary
Chapter 11. The RaspiRobot.
In this chapter, you will learn how to use the Raspberry Pi as the brain for a simple robot rover. The Pi will take commands from a wireless USB keyboard and control the power to motors attached to a robot chassis kit. The Robot will also optionally have an ultrasonic range finder that will tell it how far away it is from obstacles and an LCD screen attached to the robot that displays information from the range finder.
  • What You Need
  • Phase 1. A Basic Rover
  • Hardware Assembly
  • Phase 2. Adding a Rangefinder and Screen
  • Summary
Chapter 12. What Next?
In this chapter, we provide some pointers for different ways of using your raspberry Pi and also other resources that are available for you to use, both in programming the Raspberry Pi and making use of it for interesting uses around the home.
  • Linux Resources
  • Python Resources
  • Raspberry Pi Specific Resources
  • Other Programming Languages
  • Applications and Projects
  • Summary


Page 1. ‘Its small’ should be: it’s small – well, detail is important!

Page 36. near the bottom ‘True AND True’ should read ‘True and True’. [thanks to Brian MacKenzie]

Page 49 before the first code example. There should be some more text that says: ‘The play function uses a global variable called lives_remaining that is initialized with the code: livesRemaining = 14

Page 50 near the bottom and 51 1/3 way down the page. the references to ‘print_word’ in the discussion should be to ‘print_word_with_blanks’. [thanks to Brian MacKenzie]

Page 57 near the top. ‘Python lets you do a couple of next tricks’ should read: ‘Python lets you do a couple of neat tricks’ [thanks to Brian MacKenzie]

Page 71 3/4 of the way down the page: ‘ The method simply returns the result of multiplying the value passed in by self.scale’ should read: ‘ The method simply returns the result of multiplying the value passed in by self.factor’ [thanks to Karl Cookson]

Page 117, last paragraph – “it was been developed”, should be “it has been developed” [thanks to Karl Cookson]

Page 127 last paragraph – “need two hit”, should be “need to hit” [thanks to Karl Cookson]

In chapter 4 section “Function Parameters” the line: varspan = maxInt – minInt + 1; should have a space after var.

Other Notes for the Next Edition

I should mention somewhere that the super user and user password by default is ‘raspberry’

Although IDLE indents for you, it is worth noting that it uses 4 spaces for indentation and no tabs, so beware if you use other editors.

27 thoughts on “Programming Raspberry Pi: Getting Started with Python (1st Edition)

  1. joel sampson

    Simon: I like your books. It would be great if one could download ALL the code in Programming the Raspberry Pi in one zip file.

    Thanks. joel / N5LXI in Dallas, TX, USA

  2. admin Post author

    Hi Joel,

    If you follow the link to the code from the book page you will see a button labelled <> on the righthand side of the page.

    If you click here it should open up the options which includes ‘download zip’ (at the bottom) which will allow you to download all the files in one go.


  3. Wonho So

    Thanks your book.
    I found a strange word “prepend” on page 66.
    It is third line above the second example code block.
    Which word would you like to use to explain modules?

    Good luck.

  4. Tom Wilson

    Your book read well and worked well until the start of the “Hangman” code on page 48. At that point, it ceased to make sense to me, a novice. The code, out of the context of a completed program, did not run on my raspberry pi and code I downloaded was enormously long, full of html code (which I don’t understand) and every one of these programs would gave me an error similar to the following:
    File “”, line 6

    SyntaxError: invalid syntax

    Do you have anything somewhat more elementary for someone lacking the sophistication to follow all of this? Completed examples of how the code we are supposed to write and run would be greatly appreciated.
    Tom Wilson

  5. admin Post author

    Hi Tom, if you are getting HTML in the code it sounds like you are saving the page on Github, rather than the file. Try clicking on the RAW button and then saving that, or download the whole file as a zip onto your Pi and then uncompressing it. Hope this helps. Si.

  6. admin Post author

    Prepend means to add to the front of. Sort of the opposite of “append”.

  7. Mike Alberga

    I love your books.

    I really feel like a fool. Now that I found this page, there is no one else asking about my problem.

    Hangman does not work. The downloaded code an get
    ” NameError: name ‘anything typed’ is not defined.

    _ _ _

    Lives Remaining: 14
    Guess a letter or whole word?
    type ‘c’
    guess = input(‘ Guess a letter or whole word?’)

    NameError: name ‘c’ is not defined

  8. mike alberga

    Ok, I found the issue… my fault

    IronPython for windows visual studio. Version 2.7

    on raspberry pi:
    Idle is version 2.7

    IDLE 3 is version 3

    The books states NOTE in Python 2 the “input” functions was called “raw_input”

  9. Christopher Becker

    Hi Simon, your book is really readable and useful. I’m really glad I picked it up. You offer ways to code that I haven’t seen in other introductory books on Python and I have read several. There was one paragraph that I found confusing, however. On page 57 in your discussion of tuples, you state “Python lets you do a couple of … tricks using tuples, as described in the next two subsections.” You already mentioned the typo in your errata above, but I couldn’t tell if the next two subsections (Multiple Assignment and Multiple Return Values) are really about tuples or not. I’m a beginning coder but it looked like multiple assignment was just serial assignment of several variables at once, not a tuple assignment. Regarding multiple return values, isn’t this an example of using a list, not a tuple? You even state “list = […]” Sorry if I’m completely off base here, I just didn’t see that these were the “neat tricks” you could do with tuples. Thanks again for such a great book, even with my confusion of this section.

  10. admin Post author

    Hi, Yes tuples are involved in multiple return values. I guess you could say its a bit of a cheat, because you return a tuple containing multiple values rather than multiple values per se. This is helped by being able to assigning values to more than one variable in one line, assigning the values from the tuple. Glad you are enjoying the book! Si.

  11. Celso ayr / the codes does not work , already tried several veses

  12. admin Post author

    Hi Celso,

    Have you downloaded the code or are you typing it in?

    Also, Mike Alberga (post 22/06/14) had a similar problem and it was down to which version of python he was using. Are you using version 3?


  13. Noel Leitzman

    A good getting started book. My ebook copy does not give referenced page numbers for the items listed. It seems to be next to useless. I got the raspberry to keep from becoming bored at 78 years old. I am a retired engineer who has done a little programming in Fortran, Basic and C . I’m sure I will enjoy the Pi.

  14. Ian Bullfrog

    Hi there.
    Thanks, Im working my way through your book now. I was enjoying until the beginning of the hangman project, then the explanations were a little ., well, not quite as simple. I am familiar with some programming stuff and found it a little confusing but it did make me think :) as Tom Wilson said above.
    the Inclusion of an explanation of play() would have been pretty useful! as I forgot to write a function to… all the functions and run it :) oops.

    aswell as that. IM now playing with and arduino and the serial port.

    I cannot get the book example to run.
    ser.write(‘5) gives errors, however I found that using

    ser.write(bytes, (‘5″, encoding = “ascii’))

    the errors are relating to data type str or something.
    with the above code it returns 1 in blue

    the LED blinks. woo hoo!

    I have noticed that after the initial setup
    ser = serial.Serial(‘/dev/ttyACM0’, 9600)

    the Arduinos Tx LED flashes constantly . promising
    and the pin 13 LED flashes nearly instantly after sending the ser.write command…for a while
    then the TX light stays on….. and there is ….a………..long…..delay, before it responds.

    any ideas on why this could be?

    and now, to carry on with your book :)

  15. Ian Bullfrog

    and… i was going mad!

    I had the program working….except that…. if I entered the First letter of the word in hangman… it said Well done, you have won!

    it too me ages to figure that out as the rest of the program worked perfectly.
    …indentation! gah!

    the FINAL LINE….. if you indent the Return one more tab….so its und the IF… mistake, then, THAT is the result….2 days, nearly 3! :( meh!

  16. admin Post author

    Humm, the book is getting due for an update. Are you using Python 2 or Python 3?

    As for the Arduino, are you using a Leonardo? If so, add this line after Serial.begin()

    while (!Serial) {};

    Unlike the Uno, the Leo serial start command does not block.

  17. Aroni

    Hi Simon,
    I was trying to use the code from github fro the raspirobot . I get the following error when I try to use the code. I am using a pi version b+. Can you please suggest what am I doing wrong. I downloaded the latest tar rrb2-1.1.tar.gz and did the install .
    >>> from rrb2 import *
    >>> rr = RRB2()
    Traceback (most recent call last):
    File “”, line 1, in
    NameError: name ‘RRB2’ is not defined
    >>> rr = RRB2(revision=2)

    Thank you.

  18. Ruud Schmeitz

    Hi Simon ,

    I just started reading the Kindle version of your book , and I have a question about the example you give in the Numbers paragraph of Chapter 3 – Python Basics :

    >>> 20 * 9 / 5 + 32
    However, this example does tell us a few things:
    • Python does multiplication before division, and it does division before addition.

    I don’t think that’s entirely true . You could also draw the conclusion that Python does the calculations from left to right .

    Perhaps a clearer example would be :
    >>> 32 + 9 / 5 * 20

    Doing the calculation from left to right results in 164 .

    Ruud Schmeitz

  19. John van Someren

    The book (my copy says ‘Copyright 2013’) is aimed pretty well perfectly at someone of my skills. Many thanks.
    However, I have cursed and sworn at the book a dozen, dozen times because it *will not* stay open at the page I am typing in from. Of course I could download the files, but I learn much, much better if I type in each line for myself. Of course, I could get the e-book version, but I so much prefer paper, and the Post-It notes I stick in as markers.

    So, author, editor, publisher, please, please bind the book so that it stays open at the page I am reading, just like every ordinary paperback novel. I have met mousetraps that shut more slowly than this book when provoked :)

    Best regard

    PS Can you add me to your mailing list so that I hear when a later edition comes out for the current hardware version?

  20. Chris

    Im useing python 3.2.3 , and none of the codeing seems to be working iv got the booked followed everything, so i need python3.1.3 ? if so how do i get it ?

  21. Forrest Erickson

    Dear Admin,
    Looks like my example code in the previous submission does not post well as the blog is trying to intrepret the HTML.

  22. Mike Watson

    how do I download the program text from your repository on GitHub en mass straight into my raspberry pi? Thanks Mike

  23. Pingback: Ray Ban Sunglasses

Leave a Reply