Iteration

I am genuinely delighted with the [Codecademy][] course on Python: there are a number of basics to programming that I was having a hard time grokking while reading the various books on programming. I don’t think it’s really a matter of book versus interactive website, however. I think it’s just that the folks who wrote the Codecademy course, perhaps in the process of trying to develop something interactive, stumbled upon the sticky points for newbies.

One of those fortunate stumbles was the concept of *iteration*, which was not something I could figure out on my own. I of course had poured over any number of Python scripts, trying to understand how it was people were able to tell python really nifty things like:

for word in words:
do this

How did Python know what a `word` was? Did the script tell it that some place else? Was it part of something the script imported? Was it black magic? It turns out that Python has a built-in sense of iteration that is characters for strings, items in a list, keys in a dictionary. What you call the iterator in a loop is fairly unimportant, Python knows what to look for depending on the type of object with which you are working, the utility of the label you give the iterator is in how easy it is for you to remember when you type it again, and perhaps again, in your description of the actions to be performed (within the loop).

The coders among you are thinking? What. Is. Your. Deal? That. Is. So. Obvious.

Maybe so. Consider me a thick-skulled Cro Magnon. The humbling nature of this experience has been really instructive. I hope to bring it to bear on future students and readers: all the things you should not assume.

[Codecademy]: http://www.codecademy.com/

Leave a Reply