-
Book Overview & Buying
-
Table Of Contents
Python Illustrated
By :
In this chapter, we got a little closer to writing actual programs when we learned how to use loops. Loops make it possible to run a block of code multiple times, without having to repeat the same code over and over.
We first looked at the while loop. A while loop executes a block of code as long as a certain condition is True. Pretty simple, but you do have to watch out for accidental infinite loops. This is what happens when the condition remains True forever, and the code block just keeps getting executed. Just make sure your condition eventually evaluates to False, and you’ll be fine. Oh, of course, we also studied the correct syntax of the while loop:
while condition_is_true:
# Code to execute repeatedly
Things got even more exciting when I got to tell you what the for loop is for. A for loop iterates over a sequence and executes a block of code for each item in the sequence. Sequence? We mean a list or a tuple, for example. This is what the syntax...