Often, we want to create a list of consecutive numbers. For example, suppose we want a list containing the numbers 0 through 9:
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Typing out all of those numbers takes time and the more numbers we type, the more likely it is that we have a typo.
Python gives us an easy way of creating these lists using a function called range
. The function range
takes a single input, and generates numbers starting at 0
and ending at the number before the input. So, if we want the numbers from 0
through 9
, we use range(10)
because 10
is 1 greater than 9
:
my_range = range(10)
Just like with zip
, the range
function returns an object that we can convert into a list:
>>> print(my_range) range(0, 10) >>> print(list(my_range)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Instructions
Modify list1
so that it is a range containing numbers starting at 0 and up to, but not including, 9.
Create a range called list2
with the numbers 0 through 7.