Ranges

CS 65/STEM 280: Introduction to Computer Science I

The range() function

Let's take a look at the range() function

In [1]:
range(1,10)
Out[1]:
range(1, 10)

A range is a new type - it's a sequence like a list

In [2]:
type(range(1,10))
Out[2]:
range

range(start,end) creates a sequence of numbers that begin with start and ending just before end

In [3]:
list(range(1,10))
Out[3]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

For loops with range()

The range() function is often used with for loops to create counter-controlled loops where you don't have to explicitly manage the counter.

In [4]:
for i in range(1,10):
    print("This is iteration",i)
This is iteration 1
This is iteration 2
This is iteration 3
This is iteration 4
This is iteration 5
This is iteration 6
This is iteration 7
This is iteration 8
This is iteration 9

Arguments to range()

The range() function can also take 1 or 3 arguments instead of just 2.

When used with 1 argument, the start is implied to be 0.

In [7]:
for i in range(10):
    print(i)
0
1
2
3
4
5
6
7
8
9

When used with 3 arguments, the third argument tells it what to increment the counter by.

In [12]:
for i in range(1,10,2):
    print(i)
1
3
5
7
9

You can even use this to count backwards.

In [13]:
for i in range(10,0,-1):
    print(i)
print("Blast off!")
10
9
8
7
6
5
4
3
2
1
Blast off!

Group Discussion

For each of the following loops, predict what will be displayed, then run it to see if you were right.

In [ ]:
for num in range(1,5):
    print(num)
    
for num in range(5,10):
    print(num)
    
for num in range(0,5,2):
    print(num)

for num in range(5,2,-1):
    print(num)
    
for num in range(5):
    print(num)
    
for num in range(9,10):
    print(num)
    
for num in range(10,10):
    print(num)
    
for num in range(1,10,100):
    print(num)

Using for loops with range() and lists

range() is often used with a for loop when you prefer to access the items using an index

For example, consider this while loop we wrote in a previous lab.

In [14]:
rainfall = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]

counter = 0
while counter < len(rainfall):
    print("Day",counter,"had",rainfall[counter],"inches of rainfall.")
    counter += 1
Day 0 had 0.0 inches of rainfall.
Day 1 had 0.3 inches of rainfall.
Day 2 had 0.71 inches of rainfall.
Day 3 had 0.0 inches of rainfall.
Day 4 had 0.32 inches of rainfall.
Day 5 had 1.1 inches of rainfall.
Day 6 had 0.4 inches of rainfall.

If we want to do this with a for loop, we lose the ability to print the day number. We would have to explicity keep track of a counter again - so then we might as well just use a while loop.

In [15]:
rainfall = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]

for amount in rainfall:
    print("Day ? had",amount,"inches of rainfall.")
Day ? had 0.0 inches of rainfall.
Day ? had 0.3 inches of rainfall.
Day ? had 0.71 inches of rainfall.
Day ? had 0.0 inches of rainfall.
Day ? had 0.32 inches of rainfall.
Day ? had 1.1 inches of rainfall.
Day ? had 0.4 inches of rainfall.

Instead, we could make a range() with all the indices of our list by using the len() of the list:

In [16]:
list( range(len(rainfall)) )
Out[16]:
[0, 1, 2, 3, 4, 5, 6]
In [18]:
for counter in range(len(rainfall)):
    print("Day",counter,"had",rainfall[counter],"inches of rainfall.")
Day 0 had 0.0 inches of rainfall.
Day 1 had 0.3 inches of rainfall.
Day 2 had 0.71 inches of rainfall.
Day 3 had 0.0 inches of rainfall.
Day 4 had 0.32 inches of rainfall.
Day 5 had 1.1 inches of rainfall.
Day 6 had 0.4 inches of rainfall.

In summary

When using a for loop to iterate through a list, always determine which you are doing:

  • do I want the loop variable to be the items in the list
    • then don't use range()
    • don't use [] notation for your list, just use the loop variable
  • do I want the loop variable to be the indices of the list
    • then use range(len(name_of_list))
    • use [loop_counter] to access items in the list
In [19]:
rainfall = [0.0, 0.3, 0.71, 0.0, 0.32, 1.1, 0.4]

for amount in rainfall:
    print("Day ? had",amount,"inches of rainfall.")
    
for counter in range(len(rainfall)):
    print("Day",counter,"had",rainfall[counter],"inches of rainfall.")   
Day ? had 0.0 inches of rainfall.
Day ? had 0.3 inches of rainfall.
Day ? had 0.71 inches of rainfall.
Day ? had 0.0 inches of rainfall.
Day ? had 0.32 inches of rainfall.
Day ? had 1.1 inches of rainfall.
Day ? had 0.4 inches of rainfall.
Day 0 had 0.0 inches of rainfall.
Day 1 had 0.3 inches of rainfall.
Day 2 had 0.71 inches of rainfall.
Day 3 had 0.0 inches of rainfall.
Day 4 had 0.32 inches of rainfall.
Day 5 had 1.1 inches of rainfall.
Day 6 had 0.4 inches of rainfall.

It's really easy to mix these up - be ready to look for this if your loops are working.

In [22]:
for x in rainfall:
    print(rainfall[x],"inches of rainfall.")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-2abd1659805b> in <module>
      1 for x in rainfall:
----> 2     print(rainfall[x],"inches of rainfall.")

TypeError: list indices must be integers or slices, not float