Simple Python programs

When launching an after school Raspberry Pi club, it is very useful to have some simple programming exercises for the kids to do to keep them busy for the first week or two, until they work out what projects they want to work on and get started.

Also, I have noticed that some class teachers and technicians who set up after school Raspberry Pi clubs might not always have the experience or the time to engage very deeply with Python – but they might have done a bit of programming in Visual Basic or some other visual environment.

Below is a zip file that you can download with some very simple staged python programs – there are many ways that you can use these, but here are some suggestions. The ‘name’ programs here were entirely inspired by a podcast I had heard by Alan O’Donohoe (@teknoteacher) of some exercises he used with his GSCE computing students.

Write your own exercise sheets

Use the python programs name1 to name6 as the basis for some simple staged exercises, these could be one task per worksheet, or as a series of exercises on a multi-week worksheet. You can use these python programs as the basis for a model answer.

Each of the programs has many additional angles that you can interject to make sure that you stretch the really fast kids, or make sure that the not so fast ones really deeply understand a concept before moving on, so you can deal with a wide range of abilities from these simple programs.

As a cookbook for the kids

If you are not teaching python through a taught course, but getting the kids to work on projects, there will be a time when they want to know how to use an ‘if’ statement or a ‘while’ loop. Print these program listings out and file them in a little book that they can refer to. I prefer not to store them online, as there is significant value in avoiding “cut and paste” and getting the kids to type the programs in themselves.

Some kids like to type in these programs first and then modify them to make sure they understand the concepts, others just like to flick through the book looking for example program snippets they can then re-purpose in their own programs.

As a cookbook for the adults

As I said at the start of this blog post, you might be a busy teacher or technician with particular skills, but your programming experience might be limited, or you might be coming from a different language and need some hints on the specific python syntax.

Print these out and file them in a book to take a shifty look at when you are asked a question by the kids, or store them on your iPad or tablet and look through them when you need a memory jogger.

Also, if you are new to programming, or new to python, you might want to work through each of the programs yourself prior to starting a club as a way to familiarise yourself with the core language concepts.

Timing Example

timing.py is the first example that uses an ‘import’ statement, and is the point at which you can introduce other libraries of pre-written code. It is a simple counter, but there are lots of ideas for development here, such as turning this into a clock.

A simple clock could ask the user to enter the time in hours, minutes an seconds, and then count up from there. There is some lovely maths with division and modulus to cope with hours minutes and seconds if you store the time as a number of seconds offset into the day, or there are some if statements required if you do it with 3 variables and handle the carry conditions manually.

You can introduce validation into the time entry routine which throws up some interesting range issues for each field, or you could get the kids to display a little menu with different button presses advancing the hours, minutes and seconds with each press just like a real clock.

You could introduce an alarm time, and when the alarm goes off it could display a message on the screen.

If you integrate the code with the graphics examples below, you could get the kids to draw 4 7-segment LED displays in red that are updated to display the numbers (complete with the flashing colon in the middle!), and the more advanced kids might enjoy the maths involved with drawing slanted diamond shapes rather than just rectangles.

You can introduce functions, so a function that draws a 7 segment pattern at a position of the screen, and then layer those into functions that display hours minutes and seconds.

And finally, you could lead into a hardware project, where you fit 4 * 7-segment LED displays to the GPIO pins (at which point you run out of pins and have to introduce a real-time scanning routine to scan through 4 commons of 7 segments rapidly to introduce persistence of vision – which is how real clock displays work).

As a bonus, it should be possible to change 1 line of code and switch between the software on screen simulation and the real hardware. So there are lots of places to go from this simple program. Read the blog articles about the dice game for a lot of the hardware related knowledge, and also to (soon) read about our on screen software simulation of the dice which used the (below) graphics examples as a starting point.

Graphics Examples

There are two programs graphics1.py and graphics2.py that give you the bare minimum to open up a graphics window and plot pixels on the screen. graphics1.py just opens a window and plots a small box, whereas graphics2.py is a stepping stone towards “Conway’s game of life” as recently popularised by Amy Mather (13, @MiniGirlGeek) at the CPC Raspberry Jamboree 2013. See the linked article below for full details of Conway’s game of life.

Graphics2.py plots a grid that it fills with 500 random points, then it clears the grid and plots another 500 random points. It creates an interesting “shimmering” pattern on the screen that can be useful to run up on the Pi as kids enter the room so that the screen is doing something active. Again, you can use the graphics cookbook examples in all the ways described above for the name.py programs.

Resources

[1] SimplePython.zip

[2] Mather, A. “Conway’s Game of Life“, 2013 Raspberry Jamboree, Manchester.

[3] Conway’s Game of Life on wikipedia, accessed 06/04/2013

[4] A dice simulator

This entry was posted in How to, Python, Raspberry Pi. Bookmark the permalink.

Comments are closed.