With a for loop, the loop variable is bound, on each iteration, to the next item in a sequence. Sometimes, it is natural to think about iterating through the positions, or indexes of a sequence, rather than through the items themselves.
For example, consider the list ['apple', 'pear', 'apricot', 'cherry', 'peach']. βappleβ is at position 0, βpearβ at position 1, and βpeachβ at position 4.
In order to make the iteration more general, we can use the len function to provide the bound for range. This is a very common pattern for traversing any sequence by position. Make sure you understand why the range function behaves correctly when using len of the list as its parameter value.
In some other programming languages, thatβs the only way to iterate through a sequence, by iterating through the positions and extracting the items at each of the positions. Python code is often easier to read because we donβt have to do iteration that way. Compare the iteration above with the more βpythonicβ approach below.
If we really want to print the indexes (positions) along with the fruit names, then iterating through the indexes as in the previous versions is available to us. Python also provides an enumerate function which provides a more βpythonicβ way of enumerating the items in a list, but we wonβt cover that in this introductory course.