Lesson 03-05: The range() Function

Learning Target: I can use the range function to generate a specific list of numbers.

In the last lesson, we learned about basic for loop syntax, using the range() function. But what does the range() function actually do?

A Basic range()

If we print list(range(5)), we’ll see the following (don’t worry about list yet, that’s just how we visualize it):

[0, 1, 2, 3, 4]

If we print list(range(8)), we’ll see the following:

[0, 1, 2, 3, 4, 5, 6, 7]

Notice a pattern?

    u0305-1: What will list(range(11)) be?
  • (A) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  • In the examples above, what do you notice about the last number compared to the number in range?
  • (B) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • (C) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

So the range() function returns a list of numbers, which we use the for loop to loop through. This is very powerful, because it allows us to specify almost any range of numbers.

Generating Ranges

The range() function can take anywhere between one and three numbers. Let’s review how each one works.

With range(b)

The rule for range(b) should be familiar. range(b) can be referred to as range(end).

Using range(end) will produce a list of integers starting at 0 and ending at end-1.

With range(a,b)

Let’s look at a few examples when we feed the range() function two numbers:

Notice the pattern? Let’s see if you got it:

    u0305-2: What will list(range(3,8)) be?
  • (A) [3, 4, 5, 6, 7, 8]
  • In the examples above, what do you notice about the first and last number compared to the two numbers in range?
  • (B) [4, 5, 6, 7, 8]
  • In the examples above, what do you notice about the last number compared to the two numbers in range?
  • (C) [3, 4, 5, 6, 7]
  • (D) [4, 5, 6, 7]
  • In the examples above, what do you notice about the first number compared to the two numbers in range?

A range() function with 2 numbers can be referred to as range(start, end)

As a general rule for range(start,end), Using range(start,end) will produce a list of integers starting at start and ending at end-1.

With range(a,b,c)

Let’s look at a few examples when we feed the range() function three numbers:

Notice the pattern? Let’s see if you got it:

    u0305-3: What will list(range(10,-8,-3)) be?
  • (A) [10, 7, 4, 1, -2, -5, -8]
  • (B) [10, 7, 4, 1, -2, -5]
  • (C) [7, 4, 1, -2, -5]

Hopefully you noticed that the third number determines how much the next number in the list will change by. This is called the step. A step can be positive or negative.

A range() function with 3 numbers can be referred to as range(start, end, step)

As a general rule for range(start,end,step), Using range(start,end,step) will produce a list of integers starting at start, changing by step with each number, and stopping before we get past end.

Checks For Understanding

Q#1

Come up with a range function that will produce the following numbers: [-2,-1,0,1,2,3,4] Make sure you aren't including any list conversions! Try again!

Q#2

Come up with a range function that will produce the following numbers: [1,4,7,10,13,16,19,22] Make sure you aren't including any list conversions! Keep in mind how much the numbers are changing by!

Next Section - Lesson 03-06: Iterating Over a Range