Python for Programmers: Ranges
Welcome to the Ranges lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
Range objects are data types that represent a range of integers, like "0, 1, 2, 3".
range(end)gives us all integers from 0 up to (but not including)end. In the next example, we convert a range into a list to see what it contains.>
list(range(4))Result:
Sometimes we use
rangeto generate a list of integers that we need. But often, we loop over a range to do something a certain number of times, without actually using the individual integers. For example, iterating overrange(3)does something 3 times.(Remember that
_is a conventional name when we don't plan to use a variable's value.)>
total = 0for _ in range(3):total += 2totalResult:
6
In that case, we could get the same effect by doing
for _ in [0, 1, 2]. But that doesn't work when the number of iterations is controlled by a variable.>
iterations = 5total = 0for _ in range(iterations):total += 2totalResult:
10
rangetakes two more optional arguments. First, we can provide a number to start at.range(start, end)gives us numbers fromstarttoend - 1.>
list(range(-2, 3))Result:
>
list(range(2, 5))Result:
[2, 3, 4]
Second, we can provide a
stepargument. By default,stepis 1: each number is 1 more than the previous number. When we provide astep, each number in the range is separated by that amount.>
list(range(1, 6, 2))Result:
>
list(range(1, 8, 3))Result:
[1, 4, 7]
Step arguments can be negative. In that case, the value decreases each time. Note that the range still doesn't include the
endindex.>
list(range(5, 2, -1))Result:
>
list(range(3, 2, -1))Result:
[3]
The
stepcan be any integer. For example, ifstepis -10 then the range subtracts 10 each time.>
list(range(50, 20, -10))Result:
>
list(range(5, 2, -2))Result:
[5, 3]
In many of these examples, we converted ranges into lists, since that's an easy way to see what's inside them. But ranges aren't lists; they're a different data type.
>
isinstance(range(4), list)Result:
False
Although ranges aren't lists, they do support some familiar list operations. For example,
range1 == range2is true when both ranges have the same elements.>
range(3) == range(0, 3)Result:
True
>
range(4) == range(1, 4)Result:
False
We can get the number of elements in a range with
len(some_range).>
len(range(2, 5))Result:
3
We can also use the
inoperator to decide whether a number is within a range.>
2 in range(3)Result:
True
>
5 in range(3)Result:
False
We can use the regular indexing operator to index into ranges.
>
range(3, 5)[1]Result:
>
range(2, 9, 2)[2]Result:
6
Finally, we can slice ranges to get a part of a range. For example, in a moment we'll slice
range(-5, 5)with[2:5]. This can be tricky to think about, so we'll start by looking at the whole range, then do the slice.>
list(range(-5, 5))Result:
Since we're slicing at
[2:5], we need to know what values the range has at its indexes 2 and 5. We can see them in the list above.>
[range(-5, 5)[2],range(-5, 5)[5],]Result:
[-3, 0]
Python will build a new range, using those values as the start and end arguments. The slice below is the equivalent of
range(-3, 0).>
my_range = range(-5, 5)sliced_range = my_range[2:5]list(sliced_range)Result:
[-3, -2, -1]
>
range(-5, 5)[2:5] == range(-3, 0)Result:
True