August 2019
Beginner
482 pages
12h 56m
English
The for loop is the classical form of the loop—it literally goes over an iterable (collection of values) and performs the same computation for each value, consecutively. The code within the loop may or may not be using the value for which it is computed.
For example, if we just need to run some specific code N times, we can run the loop over the range object that contains N values. Consider the following example:
>>> for i in range(3):>>> print(i)012
Here, we execute a built-in range function (which we discussed in Chapter 3, Functions) and run a loop, printing each element in this loop.
Any other iterable would suffice as well, including strings, sets, lists, tuples, dictionaries, and generators. All the code in the scope of ...