Loops and more loops#

Loops again? And again and again. Because looping is all about repetition. Every interesting thing a computer can do involves loops. The basic operations a computer can carry out are extremely simple. Complex operations are always performed by performing a lot of simple operations in some kind of loop.

Parallel Lists#

Before jumping into indefinite loops, we will take a look at a data structure called parallel lists or parallel arrays. This is not a new data type in Python, but rather a way of organizing data using the list type you already know. It is a structure used in this week’s project.

Indefinite Loops#

In Chapter 3 we considered loops through collections, especially lists. Most of our loops took the form of doing something with each item, one by one, such as counting or summing the items. We also looked at creating a summary (such as a count) for each value found in a collection (e.g., a count of the number of students in each major). In this section we’ll look at some loops that are not so easy to fit into the “for each x do p” form.

Successive Approximation#

Indefinite loops are often used in an algorithmic technique called successive approximation. The basic logic of successive approximation is

make an initial guess
while it's not good enough: 
      improve it
Voila!  Good enough answer! 

Of course the “make a guess”, “not good enough”, and “improve it” steps may require a little more refinement. Our wildfire clustering project instantiates “make an initial guess” as “randomly assign fires to clusters”, “improve it” as “reassign based on proximity”, and “not good enough” as “assignments are still changing, so maybe we can do better.”