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' hi >>> print 3 3 >>> print 2 + 2 4 >>> 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 play_again='Y' while play_again=='Y': print "pick a number" answer=random.randint(0,100) guess=input() num_guesses=1 while guess!=answer: num_guesses=num_guesses+1 if guess<=answer: print "higher than ", guess if guess>=answer: print "lower than ", guess if guess>=answer-2 and guess<=answer+2: print "almost" guess=input() print "great! The correct number was ", answer print "your number of guesses was ", num_guesses print "play again? 'Y'/'N'" play_again=input()