|Ned Batchelder : Blog | Code | Text | Site|
Kim's Python lesson
» Home : Blog : December 2012
I spent the afternoon on Thanksgiving teaching my 14-year-old niece Kim about programming in Python. She hadn't done any programming before, but was very interested, so I figured we could just give it a shot. I didn't have a text to follow, and hadn't known she was interested, so I hadn't prepared anything.
But the path we took seemed to work, so here's what I remember of it, in rough outline form. As you look at the code samples, remember the goal here isn't to use the best or most Pythonic way to write the code, but to use simple tools to teach beginning programming.
The text below is not for the teacher to read to the student, or for the student to read on their own. This is just of list of topics we discussed, and programs we wrote.
• • •
What is a computer program? Instructions for a computer to follow. Computers are really dumb, so you have to spell out everything precisely.
Install Python (IDLE doesn't work well on a Mac, we ended up with plain-old textedit and a terminal.)
Try some arithmetic in the interactive prompt. Assign values to names:
"x = 1" in Python is not like "x = 1" in algebra. You can't say, "x + 1 = 2" in Python, and you can't say "x = x + 1" in algebra.
Create and run a simple program:
You can ask for input from the user:
If statements let you make decisions:
An else clause lets you go either way:
An elif clause lets you keep trying different conditions:
We've used two kinds of values: numbers and text, called strings. You have to be clear which kind you have. 12 != "12".
Loops: when you want to do something over and over, you can use a while loop. It checks a condition and if the condition is true, runs the statements, then checks the condition again etc, over and over, until the condition is false.
Also, we can write "count = count - 1" as "count -= 1"
Play around with that first number. Computers can do a lot of boring stuff really quickly.
Let's write a program to add up all the numbers up to 1000. You have to think like a computer, which means you can't just say, "Add them all up," you need to break it down into smaller steps.
Now let's add a twist: let's only include the number in the sum if the number is divisible by 3 or by 5. To see if a number is divisible by 3, you can test if N % 3 == 0. "%" gives you the remainder after dividing, and "==" is how you test for equality.
This program is where we start to get opportunities for real logic errors. If they happen, make sure you talk about them and how they happened before moving on.
Try a new kind of value: lists. In the interactive prompt, try this:
Lists can hold any kind of value, like text strings:
Let's write a program to catalog cats (Kim has 5 cats). It will ask the user for the name of a cat, and keep asking until the user types nothing, then will show how many cats the user has.
If you want to loop forever, or test a condition that you can't test immediately, you can use "while True". Remember while tests its condition and runs the loop if the condition is true. True is always true. Later, if you want to end the loop, you can use the "break" statement.
Hmm, it looks kind of funny if you only have one cat, because it says, "You have 1 cats". Fix that.
We'll add one more feature to our cat cataloguer: print the names of the cats. There's another way to loop in Python beside "while". A "for" loop will go around the loop once for each element in a list. We can use it to print the names of the cats:
Now we make a mighty leap to madlibs, which isn't that much more complicated, but needs a few more new concepts.
Play in the interactive prompt with building a string by adding strings together:
If we have a list of things, we can examine individual elements in the list:
To do madlibs, the story will be represented as a list of pieces, where each piece can be a literal piece of text in the story, or a slot to be filled in. A piece will be a 2-element list: for text, the first element will be 'text', and the second element will be the actual text to go in the story. For a slot, the first element will be 'slot', and the second element will be the prompt for the user. The madlib will be a list of these little lists:
When writing a madlib this way, it's really hard to get the spacing and punctuation right...
The madlib program will scan the madlib. Each 'text' piece will be added to the story we're building. For each 'slot' piece, we'll prompt the user to give us someething to go in the slot, and we'll add their answer to the story. When it's all done, we'll print the story.
• • •
At this point, the student has a program they can actually enjoy. If all has gone well, they've probably wanted to show off each program to nearby admirers (typically parents), but the madlibs program is the crowning acheivement.
By the way, the madlibs exercise is one I first did with my son Max when he was 13, and wrote about then, with more code and other ideas about how to expand it.
Working with Kim on this was really interesting. There were some parts where I was astonished that she understood so quickly, and then others where something she had understood in the previous program needed to be re-explained. Programming is a really foreign environment, and it's hard for those of us who are fully steeped in it all day to realize just how foreign.