from IPython.core.display import HTML
HTML('<img src="http://teaching.software-carpentry.org/wp-content/uploads/2012/09/ControlFlow.png" height="500px">')
Computers are good at repetition. People are not.
Loops are typically constructed with "for" or "while" statements. In english, we might say something like "I could eat all of the apples in this basket."
In python, we might instead use a "for loop" to say:
basket = ["macintosh", "pink lady", "granny smith", "fuji", "spartan"]
for apple in basket :
print "I ate a " + apple
I ate a macintosh I ate a pink lady I ate a granny smith I ate a fuji I ate a spartan
Alternatively, we could say the same thing using a while loop :
while basket :
print "I ate a " + basket.pop(0)
I ate a macintosh I ate a pink lady I ate a granny smith I ate a fuji I ate a spartan
...
Iterators indicate the items of a data structure over which the for or while statement indexes itself. In the for loop above, an iterator for a list data structure was used implicitly when we named the apple variable. Each apple was one item in the list object.
Other data structures have automatic iterators as well. Let's make a dictionary of the variants and amounts of apples in the basket.
basket_dict = {"macintosh":2, "pink lady":1, "granny smith":3, "fuji":1, "spartan":3}
for variant, amount in basket_dict.iteritems():
print "I ate " + str(amount) + " " + variant + "."
I ate 3 granny smith. I ate 1 fuji. I ate 3 spartan. I ate 1 pink lady. I ate 2 macintosh.
Other types of iterators include iterators you create yourself using functions like range() and len().
basket = ["macintosh", "pink lady", "granny smith", "fuji", "spartan"]
Sometimes, you don't want to eat all of the apples. Even in moments of extreme gluttony, I care for my housemates and try not to eat the last of a certain variant of apple in the basket. So, I'll eat all but one of each variant of apple. If there was only one of some variant of apple, of course, I won't eat any of those.
basket_dict = {"macintosh":2, "pink lady":1, "granny smith":3, "fuji":1, "spartan":3}
for variant, amount in basket_dict.iteritems():
if amount > 1 :
print "I ate " + str(amount-1) + " " + variant + "."
I ate 2 granny smith. I ate 2 spartan. I ate 1 macintosh.
The if redirection loop helps the program to do different actions in different situations. Related notions include else, elif, while, continue, and break.
Try creating a new loop in which you eat all of the spartans and all but one of everything else. You may need to learn how to use elif.
...
The conditional we used above (amount > 1) helped to define the redirection. That conditional returns a boolean (that is, True(1) or False(0)). We used the greater than logical operator: >. Other logical operators include ==, !=, <, <=, >=, &&, ||, in, and, or. While loops and if loops accept boolean statements. Here are some boolean statements. Try guessing whether they'll return true or false, then check the answer. Then, try to create some booleans of your own.
True == 1
True
True == 0
False
False == True
False
"macintosh" in basket_dict
True
"Ruby Red" in basket_dict
False
"macintosh" in basket_dict or "Ruby Red" in basket_dict
True
...