Saturday, April 16, 2011

Programming update: primes in Python

In my last post about programming I lamented the difficulty I was having downloading an interactive, basic programming package for my son. I got a lot of help from people posting in the comment thread, and decided to try out Python.  After downloading it (for free) from the Python website, I was able, with the help of a Youtube video, to see how to run it interactively on my Mac.

Since then, I discovered a great Youtube series that teaches the basics of Python in 44 short lessons (4-6 minutes each). J and I have spent the last few weeks watching these together, listening to Bucky, the Python tutor, as he typed out code in his Python shell window, and as we mimicked him in ours. 

(Bucky, by the way, has a huge number of teaching videos on his Youtube channel, TheNewBoston, including biology and geometry. He himself is a college drop-out who finds the Internet and Youtube a much more cost-effective environment for learning than college. Perhaps we'll find other ways to take inspiration from him down the road!)

J and I have now finished watching most of Bucky's videos and I've given him his first couple of programming assignments. First I had him write a simple program that would solicit an arbitrary  number and say whether it is odd or even. The next day I asked him to change his code to calculate instead whether the number is prime or composite. It took him about 20 minutes, and he needed a couple of vague guidelines from me and specific reminders from Bucky, but he did it on his own, down to his endearing garbling of "composite":

def prime():
    number = input("Tell me a number: ")
    num = 2
    while num < number+1:
        if number % num == 0:
        if num==number:
while 1==1:

Interested parties can download Python, cut and paste this directly into a .py file, and check it out.  J's next assignment, meanwhile, will be a program that outputs all the prime factors of a given number.


John said...

I don't suggest it quite yet, but as he learns more python you can probably teach him how multiple computer languages have different syntax to do the same thing, or might contain additional reserved words with different behavior. Assuming your mac, maybe in the order of Apple Script, Bash Scripting and then at some point in a few years when he is really rolling, Objective C inside of XCode.

I suggest keeping a time machine backup though... "Hmm i wonder what happens when I sudo rm -rf / ?" yeah-- the backup will come in handy.

I think it is awesome that you are doing this. Back in the 80's my mom was good with individual programs but was totally confused by DOS. I was able to reverse engineer a few .bat files to figure out how it they work and made a menu system for her to do everything she needed.. but I couldn't even imagine the resources that are available now.

Though, if I did I would have incessantly begged for a unix system so I could do "real programming" -- This is one of the reasons why OS X is truly awesome.

Have fun!

Anonymous said...


I wanted unix back in the 80s. Never did quite manage it. Every option I found was expensive. Yeah Linux for making that all way cheaper.

I did get access to a Xenix system at school, but it was a dumb version of Unix, didn't come standard with a C compiler. A local BBS though did have a Xenix with C and everything, that I subscribed to for awhile.


There needs to be a blank line between "break" and "while 1==1:" HTML probably swallowed it or something.