4.6. The range Function

In our simple example from the last section (shown again below), we used a loop to draw a square.

import turtle

for i in range(0, 4, 1):
    turtle.forward(50.0)
    turtle.left(90.0)

Each iteration of the loop the value of the variable i changes. To see it change, we can declare the variable and print it out during the loop.

In the above program the variable i has the value 0, 1, 2, and 3. These values are controlled by the three inputs to the range function, 0, 4, and 1. The first input is the start value, 0 in the above example. The start is the first value that the variable i will have. The second input is the stop value, 4 in the above example. The stop determines the last value the variable i will have. Note that the stop value is exclusive. The variable i will never be the stop value. The third input is the step value, 1 in the above example. The step is how much the variable i changes by each iteration of the loop.

Codelens will help us to further understand the way range works. In this case, the variable i will take on values produced by the range function.

(rangeme)

Suppose we want to have a sequence of even numbers. How would we do that? Easy, change the range function’s step input. For even numbers we want to start at 0 and count by 2’s. So if we wanted the first 10 even numbers we would use range(0, 20, 2).

You can also create a sequence of numbers that starts big and gets smaller by using a negative value for the step parameter.

Check your understanding

    turtle-8-1: In the command range(3, 10, 2), what does the second argument (10) specify?

  • Range should generate a sequence that stops before 10 (including 9).
  • Range will generate the sequence 3, 5, 7, 9.
  • Range should generate a sequence that starts at 10 (including 10).
  • The first argument (3) tells range what number to start at.
  • Range should generate a sequence starting at 3 that stops at 10 (including 10).
  • Range will always stop at the number in the sequence before (not including) the specified limit for the sequence.
  • Range should generate a sequence using every 10th number between the start and the stopping number.
  • The third argument (2) tells range how many numbers to skip between each element in the sequence.

    turtle-8-2: What command correctly generates the sequence 2, 5, 8?

  • range(2, 5, 8)
  • This command generates the sequence with just the number 2 because the first parameter (2) tells range where to start, the second number tells range where to end (before 5) and the third number tells range how many numbers to skip between elements (8). Since 10 >= 5, there is only one number in this sequence.
  • range(2, 8, 3)
  • This command generates the sequence 2, 5 because 8 is not less than 8 (the specified number past the end).
  • range(2, 10, 3)
  • The first number is the starting point, the second is past the last allowed, and the third is the amount to increment by.
  • range(8, 1, -3)
  • This command generates the sequence 8, 5, 2 because it starts at 8, ends before 1, and skips to every third number going down.

    turtle-8-3: Which range function call will produce the sequence 20, 15, 10, 5?

  • range(5, 25, 5)
  • The step 5 is positive, while the given sequence is decreasing. This answer creates the reversed, increasing sequence.
  • range(20, 3, -5)
  • Yes: If we take steps of -5, not worrying about the ending, we get 20, 25, 10, 5, 0, .... The limit 3 is past the 5, so the range sequence stops with the 5.
  • range(20, 5, 4)
  • The step 5 is positive so the sequence would need to increase from 20 toward 4. That does not make sense and the sequence would be empty.
  • range(20, 5, -5)
  • the sequence can never include the second parameter (5). The second parameter must always be past the end of the range sequence.

    turtle-8-4: What could the second parameter (12) in range(2, 12, 4) be replaced with and generate exactly the same sequence?

  • No other value would give the same sequence.
  • The sequence produced has steps of 4: 2, 6, 10. The next would be 14, but it is not before the limit 12. There are other limit choices past 10, but not past 14.
  • The only other choice is 14.
  • 14 would work: It is also past 10, and not past 14, but there are other integers with the same properties.
  • 11, 13, or 14
  • Yes, any integer past 10, and not past the next step at 14 would work.
You have attempted of activities on this page
Next Section - 4.7. The Accumulator Pattern