February 03, 2010

A few days with my son and Python

I’ve had the Hello World! Computer Programming for Kids and Other Beginners book for about a week now, and my son and I have spent a few hours dabbling with it. So far I’d have to say that I’m pretty pleased with his level of interest.

The first few chapters are designed to get the reader to the general concept of computer programming and how to use the development environment (which is IDLE, a basic Python interactive shell). The book walks you through a bunch of basic string and number manipulating commands, and then the first chapter ends by asking the reader to type in a slightly longer program (about 18 lines) to run as a demo of concepts to be learned later in the book. The program, a pirate-themed random-number guessing game, is then run in the IDLE shell.

Watching my son do this reminded me of when I was a kid, typing a 100+ line BASIC program into a friend’s Commodore64 cassette-tape drive, executing, and hoping I hadn’t made a typo. Except now, a lot of the tedium has been removed and the feedback loop is so much faster. Ahh, kids these days just don’t realize how easy they have it…

Anyhow, as ‘cool’ as the pirate-speak is in the sample code (“What's yer guess?” and “Avast! Ye got it! Found my secret, ye did!”) my son’s interest rose exponentially once I suggested that it was his program and he could change the words if he wanted to. He decided to substitute a Pokémon theme, and spent the next 45 minutes modifying the code appropriately. Nice.

To spur my own learning and keep him interested, I created a text file with a list of all of the Pokémon characters (my some told me there are 150 of them, it turns out there are 493) and used it as a resource for another version of the number-guessing game. We were now able to have a Pokémon identify itself by name and the player guesses its ‘number’. My son spent another 45 minutes or so playing this one. Next we made another version of the app which lets the player type in the character’s name and the program will find its number in the character list. Big hit.

So far it's been baby steps for both of us, but it’s been fun. While trying to find ways to incorporate his schoolwork into the programming exercises, I made up a little quiz-game with the States and their Capital Cities. He loves it. I hope his interest continues.

I’m also enjoying learning Python. I can remember how I felt a few years ago when I made the transition from writing VBA and VB6 and ‘classic’ ASP code to writing C# and ASP.NET. Everything seemed much more stiff and restrictive, what with all of the curly braces and semicolons all over the place. One character out of place and the whole build breaks. But now I’m used to it. Dare I say, I’m almost dependent on it.

Python, however, seems so much more loose. It’s dynamically typed, and code blocks are denoted using whitespace rather than the (oh so comforting) curly braces. I don’t feel so much free writing Python code as I feel naked. When defining a class, where do I put the property definitions? “Properties?” replies Python. “We don’t need no stinking properties!”.  The way the code looks is fairly comfortable, although the sense of elegance hasn’t quite hit me yet.

I feel good about it, though. Perhaps after working my way through Hello World! with my son I’ll go a bit deeper. Maybe I can look forward to tackling Dive Into Python and/or Django.