September 2013
Intermediate to advanced
350 pages
9h 38m
English
We can also loop over a range of values. This allows us to perform tasks a certain number of times and to do more sophisticated processing of lists and strings. To begin, we need to generate the range of numbers over which to iterate.
Python’s built-in function range produces an object that will generate a sequence of integers. When passed a single argument, as in range(stop), the sequence starts at 0 and continues to the integer before stop:
| | >>> range(10) |
| | range(0, 10) |
This is the first time that you’ve seen Python’s range type. You can use a loop to access each number in the sequence one at a time:
| | >>> for num in range(10): |
| | ... print(num) |
| | |
Read now
Unlock full access