Programming Raspberry Pi: Getting Started with Python

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.
cover
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.
Contents:
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

Errata:

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]

Other Notes

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.

14 thoughts on “Programming Raspberry Pi: Getting Started with Python

  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.

    Linda

  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:
    python3.2 04_07_hangman_print_word.py
    File “04_07_hangman_print_word.py”, 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’
    Error
    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

    04_08_hangman_full.py / 04_09_hangman_full_solution.py 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?

    Linda

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>