Книга: Practical Programming, Fourth Edition
Назад: Processing Characters in Strings
Дальше: Processing Lists Using Indices

Looping Over a Range of Numbers

You can also loop over a range of values to perform tasks a certain number of times and to process lists and strings more efficiently. To begin, you need to generate the range of numbers over which to iterate.

Generating Ranges of Numbers

Python’s built-in function range produces an object that will generate a sequence of consecutive integers. When passed a single argument, as in range(stop), the sequence starts at 0 and continues up to but not including the integer stop:

 >>>​​ ​​range(10)
 range(0, 10)
 >>>​​ ​​type(range(10))
 <class 'range'>

The sequence belongs to Python’s range data type. You can use a loop to access each number in the sequence one at a time:

 >>>​​ ​​for​​ ​​num​​ ​​in​​ ​​range(10):
 ...​​ ​​print(num)
 ...
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9

To obtain the numbers from the sequence all at once, you can use the built-in list constructor to convert a range into a list:

 >>>​​ ​​list(range(10))
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Here are some more examples:

 >>>​​ ​​list(range(3))
 [0, 1, 2]
 >>>​​ ​​list(range(1))
 [0]
 >>>​​ ​​list(range(0))
 []

The sequence produced includes the start value and excludes the stop value, which is deliberately consistent with how sequence indexing works: the expression seq[0:5] takes a slice of seq up to, but not including, the value at index 5.

Notice that in the previous code, call list on the value produced by the call to range. The function range returns a range object, and creates a list based on its values to work with it using the set of list operations and methods with which you are already familiar.

The function range can also be passed two arguments, where the first is the start value and the second is the stop value:

 >>>​​ ​​list(range(1,​​ ​​5))
 [1, 2, 3, 4]
 >>>​​ ​​list(range(1,​​ ​​10))
 [1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>>​​ ​​list(range(5,​​ ​​10))
 [5, 6, 7, 8, 9]

By default, the function range generates numbers that increase successively by one, the increment referred to as its step size. You can specify a different step size for range with an optional third parameter.

Here we produce a list of leap years in the first half of this century:

 >>>​​ ​​list(range(2000,​​ ​​2050,​​ ​​4))
 [2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048]

The step size can also be negative, which makes a descending sequence. When the step size is negative, the starting index should be larger than the stopping index:

 >>>​​ ​​list(range(2050,​​ ​​2000,​​ ​​-4))
 [2050, 2046, 2042, 2038, 2034, 2030, 2026, 2022, 2018, 2014, 2010, 2006, 2002]

Otherwise, range’s result will be empty:

 >>>​​ ​​list(range(2000,​​ ​​2050,​​ ​​-4))
 []
 >>>​​ ​​list(range(2050,​​ ​​2000,​​ ​​4))
 []

It’s possible to loop over the sequence produced by a call on range. For example, the following program calculates the sum of the integers from 1 to 100:

 >>>​​ ​​total​​ ​​=​​ ​​0
 >>>​​ ​​for​​ ​​i​​ ​​in​​ ​​range(1,​​ ​​101):
 ...​​ ​​total​​ ​​=​​ ​​total​​ ​​+​​ ​​i
 ...
 >>>​​ ​​total
 5050

Notice that the upper bound passed to range is 101. It’s one more than the greatest integer you want.

Назад: Processing Characters in Strings
Дальше: Processing Lists Using Indices