Looping Over a Range of Numbers

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.

Generating Ranges of Numbers

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)
 ...
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9

To get the numbers from the sequence all at once, we can use built-in function list to create a list of those numbers:

 >>>​​ ​​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, we call list on the value produced by the call on range. Function range returns a range object, and we create a list based on its values in order to work with it using the set of list operations and methods we are already familiar with.

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, function range generates numbers that successively increase by one—this is called its step size. We 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 produces 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 we actually want.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.74.25