Python provides two loop constructs. One is the while loop, whose basic syntax is:

while expression: suite

Here is an example:

count = 10

This will print "10 98765432 1"—all on one line, due to the print statement's trailing comma. Notice that we must have a colon before the indented suite.

Loops can be broken out of prematurely, using the break statement. This is particularly helpful in loops which will not otherwise terminate, that is, because their conditional expression is always true:

while True:

item = getNextItem() if not item:

break processItem(item)

Python's while loop can also have an associated else statement using the following syntax:

while expression:

suitel else: suite2

★Andrew Kuchling, author of the "What's New in Python" documents, recommends always using parentheses with conditional expressions. In this book, we use them only when necessary.

The else clause (with its associated suite) is optional. It is executed if the loop terminates at the condition, rather than due to a break statement. It is not often used, but can be useful in some situations:

print "Found the item" break i += 1 else:

print "Didn't find the item"

The while loop is very versatile, but since it is so common to want to loop over all the items in a list, or to loop a specific number of times, Python provides an additional loop construct that is more convenient in such cases. This is the for loop, whose syntax is:

for variable in iterable:

suitel else: suite2

The else works the same as in the while loop, that is, its suite is executed if the for loop completes, but not if it was terminated by a break statement. An iterable is an object that can be iterated over, such as a string, a tuple, a list, a dictionary, or an iterator (such as a generator, covered later). In the case of a dictionary, it is the keys that are iterated over.

Here, we iterate over a string, that is, over each character in the string:

for char in "aeiou":

This prints "a=97 e=101 i=105 o=111 u=117". The variable char takes each value from the iterable in turn (in this case "a", then "e", and so on up to "u"), and for each iteration executes the associated suite.

The range() built-in function returns a list of integers that can conveniently be used in for loops. For example:

This prints "0 12345678 9". By default, the range() function returns a list of integers starting at 0, increasing by 1, up to but excluding the given value. It also has two- and three-argument forms:

Python also provides an xrange() function with the same semantics, but which is more memory-efficient in a for loop, because it evaluates lazily rather than generating the entire list of integers in one go. We will normally use range() and substitute it with xrange() only if it makes a significant difference to performance.

If the for loop's iterable is mutable (e.g., a list or a dictionary), it must not be changed inside the loop. If we want to change a list or dictionary as we iterate over it, we must iterate over a list of the list's indexes or a list of the dictionary's keys, or use a shallow copy, rather than working directly on the collections themselves. For example:

presidents = dict(Washington=(1789, 1797), Adams=(1797, 1801), Jefferson=(1801, 1809), Madison=(1809, 1817)) for key in presidents.keys(): if key == "Adams":

del presidents[key] else:

print president, presidents[key]

This removes the "Adams" key (and its associated value) from the presidents dictionary, and prints:

Madison (1809, 1817) Jefferson (1801, 1809) Washington (1789, 1797)

Notice that although Python normally uses newline as a statement separator, this does not occur inside parentheses. The same is true when we create lists in square brackets or dictionaries in braces. This is why we can spread the construction of the presidents dictionary over a couple of lines without having to escape the intervening newline with a backslash (\).

Since dictionaries hold pairs of keys and values, Python provides methods for iterating over the keys, the values, and the pairs. And as a convenience, if we simply iterate over a dictionary, we don't even have to call the keys() method to get the keys:

presidents = dict(Washington=(1789, 1797), Adams=(1797, 1801), Jefferson=(1801, 1809), Madison=(1809, 1817)) for key in presidents:

print "%s: %d-%d" % (key, presidents[key][0], presidents[key][1])

This prints (not necessarily in this order):

Madison: 1809-1817 Jefferson: 1801-1809 Washington: 1789-1797 Adams: 1797-1801

When we iterate over a dictionary in a for loop the variable is set to each dictionary key in turn.* Dictionaries are unordered, so their keys are returned in an undefined order.

To get the values rather than the keys we can use the values() method—for example, for dates in presidents.values(): and to get pairs we can use the items() method. For example:

for item in presidents.items():

print "%s: %d-%d" % (item[0], item[1][0], item[1][1])

This produces the same output as the previous example, as does the following:

for president, dates in presidents.items():

print "%s: %d-%d" % (president, dates[0], dates[1])

Here we have unpacked each pair returned by the items() method, the dates being the two-element tuple of dates.

If we want to iterate in order, we must explicitly sort the list before we iterate on it. For example, to iterate in name order we can do this:

for key in sorted(presidents):

print "%s: %d-%d" % (key, presidents[key][0], presidents[key][1])

Both for loops and the sorted() function can work on sequences or on iterators. Iterators are objects that support Python's iterator protocol, which means that they provide a next() method, and raise a Stoplteration exception when they have no more items. Not surprisingly, lists and strings implement the protocol: A list iterator returns each item in the list in turn, and a string iterator returns each character of the string in turn. Dictionaries also support the protocol: They return each of their keys in turn (in an arbitrary order). So, when we use a for loop or call sorted() on a dictionary, we actually operate on the dictionary's keys. For example:

names = list(presidents)

# names == ['Madison', 'Jefferson', 'Washington', 'Adams']

So in the for loop, we effectively called sorted(list(presidents)) which is the same as sorted(presidents.keys()). If we want to be more explicit, we could break things down into steps:

keys = presidents.keys() # Or: keys = list(presidents)


for key in keys:

print "%s: %d-%d" % (key, presidents[key][0], presidents[key][1])

* Note for C++/Qt programmers: Python's for loop iterates over a dictionary's keys, whereas Qt's foreach loop iterates over a QMap's values.

Python's sort() method and sorted() function can take additional arguments. So, for example, we could sort the presidents dictionary by dates.

In addition to the keys(), values(), and items() methods, dictionaries also provide iterkeys(), itervalues(), and iteritems() methods. These additional methods can be used just like the plain versions, and they provide better performance. However, they cannot be used to iterate over a dictionary whose keys will change during the iteration.

Just like while loops, we can use break to leave a for loop before the iterations are complete. We can also use continue in both kinds of loop to immediately jump to the next iteration. For example:

continue # goes directly to the next iteration print 1.0 / x,

This will produce output like this: "-0.2 -0.25 -0.333333333333 -0.5 -1.0 1.0 0.5 0.333333333333 0.25 0.2". Without the continue, we would eventually attempt division by zero and get an exception.

As mentioned earlier, Python's loops can have an optional else clause that is executed only if the loop completed, that is, the else clause will not be executed if break was called inside the loop. An example will make this clearer; here is an inefficient way of generating a list of primes:

break # exits the loop and skips the else else:


% When we saw the % operator earlier, it was used with string operands and operator produced a formatted string as its result. Here, we use the % operator with 24« integer operands, and in this context it performs the modulus (remainder) operation, and produces an integer as its result.

At the end, the primes list is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]. The append() method is called only if the iteration over the primes list completes, that is, if x is not divisible by any previous prime.

Was this article helpful?

0 0
YouTube Tactics

YouTube Tactics

Report Reveals 50 Easy-To-Implement YouTube Tactics For Better Videos, More Video Views and Traffic To Your Website.

Get My Free Ebook

Post a comment