For the last year or so I’ve been giving Python exercises to my 11 year old. I thought I’d share some of them. If any of you have been doing similar things, I’d love to hear what worked for you. I think it is helpful that I’m not much of a Python programmer, this forces him to read the documentation. The other day he said “Wow– Stack Overflow is great!”

## Fibonacci

Print the Fibonacci series, useful for teaching basics of looping.

## Number Guessing Game

The user thinks of a number between 1 and 100. The computer tries to guess it based on feedback about whether the previous guess was too high or low. This one was great for learning about the kinds of off-by-one errors that one customarily runs into while implementing a binary search.

## Binary Printer

Print the non-negative binary integers in increasing order. This one forces some representation choices.

## Palindrome Recognizer

Recognizing “A man, a plan, a canal — Panama!” as a palindrome requires some string manipulation.

## Door Code Recognizer

Our Paris apartment building will let you in if you enter the correct five-digit sequence regardless of how many incorrect digits you previously entered. I thought replicating this behavior in Python would be slightly tricky for the kid but he saw that a FIFO could easily be created from a list.

## Monte Carlo Pi Estimator

If you generate random points in the square between -1,-1 and 1,1, the fraction of points that lie inside the unit circle will approach pi/4. The kid got a kick out of seeing this happen. Of course I had to explain the derivation of pi/4 and the formula for a circle since he hasn’t seen this stuff in school yet. Also of course we could have simply worked in quadrant one but that seemed to make the explanations more complicated.

I should perhaps add that I explained this exercise to my kid very differently than I’m explaining it here. We started by drawing a box containing a circle and then pretended to throw darts into the box. The concepts are not hard: all we need to understand is random sampling and the area of a circle. Math gets made to seem a lot harder than it is, in many cases, and also we tend to conflate the difficulty with the order in which the concepts are presented in the school system.

## Turtle Graphics Interpreter

The current project is implementing a turtle that takes four commands:

- forward n
- right-turn n
- left-turn n
- color c

So far we’ve been restricting angles to 0, 90, 180, 270 (and no radians yet…) but I don’t think sine and cosine will be hard concepts to explain in this context. Also, so far the turtle commands are just a series of function calls, the parser will come later. I wonder what would be the simplest loop construct for this turtle language? Perhaps “repeat n” followed by some sort of block construct.

## 26 responses to “Python Exercises for Kids”

Mandelbrot set, ASCII or otherwise.

There’s a nice simple graphics library called Zelle. Using that I like the lumosity game called eagle eye. A little thing flashes once at a random location in the window. The player has to click within some radius of where it flashed.

Definitely 2nd mandelbrot. Also, pascal’s triangle.

One of the first programs I ever wrote and re-wrote many times as a kid was the old “type a verb, a place name, a person’s name, an adjective, a weapon” etc. and placing those words into a story/script. That can progress on to choose-your-own-adventure type games.

Lego mindstorms and other physically interactive programming environments are also good for holding attention.

Stack Overflow is, indeed, great.

Implement various sort algos (bubble sort, quick sort, …)

There’s another way to approximate pi, by dropping matchsticks on a hardwood floor and counting the fraction that cross the groove between slats. That might also be fun to simulate, especially if you first do the experiment with a real floor if you have that kind.

If he’s doing graphics and understands trig functions, quasicrystals are beautiful and easy to generate:

http://laughingsquid.com/a-hypnotic-animation-of-a-quasicrystal-that-reveals-hidden-patterns-when-the-viewer-blinks/

http://mainisusuallyafunction.blogspot.com/2011/10/quasicrystals-as-sums-of-waves-in-plane.html

This is way too complicated. You need simpler things to start with programming, preferably things that don’t require you don’t understand another field (such as maths).

Basica calculation : My go to exercice for a start is to calculate your age given you birth year.

If/else: Then use that to check if the perso has the right to vote (or drink depending of the audience).

while, for and lists: Then I do a poll programm, asking what is your favorite color is and store them in a list and print them.

sets : Then the same without duplicates

dicts: then the same with a counter if people enter several times the same color

…

…

and you progress like that until the person can do fun stuff like analysing the browser history to check for some kind of websites (again, matching the audience…), downloading and extracting stuff from open data, etc.

I love the digicode example though, I’m going to steal it, make it a tkinter program, and ask the student just to write the validation function.

Mandelbrot and Pascal’s triangle are great ideas, thanks. Also sorting.

Paul, I remember seeing the matchstick pi estimator but this seems way harder!

LeSam, of course this is not way too complicated since he has already done all of these except for the last one. There’s a gigantic difference between what you can do in class (remember I’m a university CS teacher) and what you can do one on one.

You might want to take a look at https://www.codingame.com

It’s a French company offering programming games of increasing difficulty, which require to implement algorithms to solve.

It is free, and the games can be tackled with most programming languages, including Python of course.

Best

recommend a book called “Python for kids”. Hope it will help!

I found it fun to write programs to solve puzzles — Sudoku is one example, of course. When I was just starting to program, I heard of a knights-tour style puzzle: 10×10 grid, start in any square, visit every square once given some constraint on movement (IIRC it was 2 up/down/left/right, or 3+1). Hard to solve on paper, easy to brute force with a program!

I also wrote a 2-D “ant farm” simulator in Java — besides GUI programming, this can teach flocking & emergent behavior.

Hello,

an advice, target something graphical, my first lesson (when I was quite young) was drawing a (2D) house: I was hooked: later I added birds, sun, etc.

Later I had to implement Pascal’s triangle, beautiful but a bit ‘static’: now that computer are fast enough, a mandelbrot set renderer and then ‘navigator’ would be a very rewarding.

+1 for Paul’s recommendation. A great way to introduce basic string templating is having kids create madlibs. Have them design a template for the main story, and specify placeholders for each noun/verb/adjective, etc. Then they create a madlib “engine” that takes an arbitrary template as input, and prompts the user interactively to fill in the values.

You can further add to the lesson by introducing file I/O in the form of a library of madlib template files that can be loaded, and then optionally save the resulting story.

Yeah, Mad-libs is a great idea, we’ll definitely do that.

And thanks for the additional suggestions folks!

No, I believe LeSam is spot on about the difficulty of these. While this may work for your child I believe this would alienate many more than it would help. A child that find a Monte Carlo Pi Estimator to be an exciting problem is a precocious rarity.

There’s a reason that even intro courses at the university level use media based languages like SNAP.

“I wonder what would be the simplest loop construct for this turtle language?”

Simplest would probably be “again” – just restarting the program from start. Next thing to do could be to have the program figure out when to stop repeating (reaching a state where it already been). (which could also introduce to halting problem if one wants to go theoretical cs stuff too).

Woa, what kind of kids are these for ? Ones that are very strong at maths is my guess.

Fibonacci, we’re not just into maths but recursion and that is the first example!?

wanders– “again” is a great idea, I’ll definitely have him do that, thanks!

Stu, a lot of math is not hard until we psych kids out by telling them it’s hard. Also a lot of people find recursive solutions to be simpler than non-recursive ones. But in any case he ended up with a non-recursive Fibonacci.

Sieve of Eratosthenes prime number checker would be quite interesting.

Then maybe a large number factoriser, since Python supports bigints natively. I would have been fascinated as a ‘tween to see how long it takes to factorise numbers as they get larger. Introduction to complexity theory at an early age!

Also, I think using recursion to implement Fibonacci would be cool as it blows up the stack on relatively small values.

If the graphics package is simple and the child is persistent enough, an animation of the Towers of Hanoi would also be a good demonstration of complexity.

Recently, I’ve been coding very simple stuff with my 5-year old daughter who become somewhat keen on “roboter speech”. Perhaps she saw the colorful python snake in intellij whenever I was coding something for work or myself. Also she’s eager to visit the upcoming linux days, just because she likes tux and the cool playgrouny for kids they have there.

Anyway, we’ve been creating “guess a number” in python recently and as she is not yet ready to read and write, we used “espeak” to print and say the interactions. This is very simple but before a child can actually do (means: “understand”) a binary search, she should first have a clue what numbers and orderings are. She is playing with that and I see, that she’s happy to learn first sights of number abstractions. When she has a concrete envision of what it means to, let’s say, cut the numbers between 1 and 10 in half, the way for a simple and gentle introduction to algorithms may has arrived.

Another very simple game we want to create is “Find the letter”. The computer will say a letter (again using espeak) and the girl should find the letter on the keyboard. A whole game will consist of letters that form complete words which will successively provide hints to where the dad has placed some candy. I bet this alone will provide sufficient motivation.

I recently did “I have this GPS file from our hike. How high did we go?” They needed some help navigating the Google API to query height, but were able to figure out how to parse an XML file using common libraries, etc.

At 11 I used to enjoy messing with BEEP to make tunes. Also drawing patterns (moire, circles, graphs, line-based fractals, all kinds of plotted and coloured stuff). I remember wanting to draw 3D line objects (like Battlezone) and I had to ask my teacher about the maths for that. Mostly it was making the machine produce visible/audible stuff for my own enjoyment.

Later I moved on to Z80 assembler, wrote a simple space invaders, had fun creating my own hyperloaders (for loading/saving from tape), wrote a simple Forth dialect. I had to learn about log-scales to understand pitch. I would have loved to have something more than a 1-bit audio interface to play with. (With 8-bit audio you’d get into sine waves, harmonics, etc.) Then the 68000 machines started appearing.

I think the maths and algorithms only came in in service of other childhood aims. Isn’t that what they say about home schooling? You get to the maths eventually anyway. I remember being amazed on disassembling a commercial game at seeing how they built up their squares table with just addition. When our children get old enough, it will be the colours, shapes, noises that they can create that I hope to use to grab their attention and motivate their learning, should they show any interest at all. It’s a shame that the hardware is now several layers away from the programmer. Hitting I/O ports from assembler just makes all the workings of hardware so transparent.

I’m pretty sure a Raspberry Pi can run Python. Someone suggested a door code reader. How interesting and educational would it be to make and program an actual door lock instead of a virtual one?

One that my children enjoyed doing was a Morse Code translator

Tron should be fairly simple to implement (that is, a snake-like game where the snake will always grow). (Hint: Make it a two player game, otherwise it is somewhat boring…)

This can be expanded to a snake-like game where the snake always has a certain length (although it can be expanded when eating food).

Finally, as an exercise in recursion, a depth-first search algorithm could be used to implement an AI opponent that tries to grab the food before the player has a chance to grab the food. (Perhaps this could also introduce Dijkstra’s shortest path algorithm although the game might be more fun if the opponent will not necessarily use the shortest path.)