teaching beginning programming with Python

I’m a software engineer. I’ve been programming for a long time – in school, at work, and for fun. While I was in school, I also taught computer science as a TA for three years.

I TAed a mid-level class, though, so by the time I met the students, they didn’t just know how to program, they’d also mastered more in-depth concepts like pointers, recursion, computer architecture, and algorithms.

Last Sunday, for the first time, I had the chance to teach someone to program who’d never written a line of code before. It was very, very different from TAing. It was also a lot of fun.

I was inspired by David Bau‘s Haaarg, world!, so I decided to use Python because of its simple syntax, plain-english keywords, and interactive shell. We started in the shell with a few print statements.

>>> print 'hi'
>>> print 3
>>> print 2 + 2
>>> print 'hi', 2
hi 2

We quickly covered strings, integers, and basic arithmetic. We opened a .py file and wrote a Python script, ran it, saw the output, and cheered. Hello, world!

print 'Hello, world!'

We moved on to variables, which we thought about as little boxes in memory, with names, that could hold things. We used input() to ask the user’s name, stored it in a variable, and said hi. We did more arithmetic, this time with variables instead of integer constants.

Variables were a little tricky, but not too bad. Even better, they were the worst of it. String operations, expressions, conditionals, loops, and other control flow came easily. Even libraries and import statements didn’t cause much trouble.

Other than variables, the only speed bump was = for assignment vs. == for equality testing. It’s tough for first-timers to understand why they need to use different operators. Guido actually mentioned this himself in one of his article about teaching with Python. I tried to find a link, but no such luck.

In the end, we had a working copy of that old standard, the number guessing game. Check it out!

import random

while play_again=='Y':
    print "pick a number"
    while guess!=answer:
        if guess<=answer:
            print "higher than ", guess
        if guess>=answer:
            print "lower than ", guess
        if guess>=answer-2 and guess<=answer+2:
            print "almost"

    print "great! The correct number was ", answer
    print "your number of guesses was ", num_guesses
    print "play again? 'Y'/'N'"

One thought on “teaching beginning programming with Python

  1. Nice work! Teaching a newbie to program can be a lot of fun. I know I have enjoyed it a couple of times. And using python just makes the whole thing easier. I remember learning to program in Fortran and well … it was painful at times. Do I regret it? No, but I think it says volumes that most of the good programmers I know chose easy languages like Python, Ruby and shudder Perl for hacking stuff together on their own time.

    Next exercise- generate the Fibinocci sequence and maybe something fun like list comprehensions.

    This kinda makes me think though … algorithms are fun, but kinda pointless to most people. Most people care more about data and doing fun stuff with data. By most people, I mean me. So are there any good free data sets out there for people to play with under the pretense of learning to program?


Leave a Reply

Your email address will not be published.