7.3. More on range

In one example from the last section, we used range to make the loop run four times:

1
2
3
for num in range(4):
   print(num)
   print("Hello" * num)

For each iteration, the variable num took on a new value (0, 1, 2, or 3).

What if we wanted the loop to use the sequence 1, 2, 3, 4 instead?

7.3.1. Set Start and Stop Values

Whenever we use range(value) in a for statement, Python always begins counting with 0. To start counting at a different number, we need to include that value inside the () in addition to a stop value.

Instead of range(value), a more detailed version of the keyword is range(start_value, stop_value). The starting value is included in the counting for the loop, but the stop value is NOT.

Syntax Example for Start and Stop Values

for num in range (start_value, stop_value):
   # do something...

If we replaced line 1 in the code above with for num in range(1, 5), then the loop variable num would take values of 1, 2, 3, and 4.

Try It

Run the following code to see how using different start and stop values effects the output.

1
2
3
4
# Change the start and stop values in 'range'. How does this affect the output?

for num in range(1, 4):
   print("Hello" * num)

Now, what if we want to count DOWN from one value to another or change the loop variable by more than a single unit each iteration?

7.3.2. Set a Step Value

Suppose we want our loop variable to only be a set of even numbers (e.g. 0, 2, 4, 6…). We want to begin counting at 0 and then increase the loop variable by 2 units instead of 1.

To make this happen, we need to add one more value inside range. This is called the step value.

Syntax Example for Adding a Step Value

for num in range(start_value, stop_value, step_value):
   # do something ...

Examples

To count from 0 to 20 by 2’s, use:

for num in range(0, 21, 2)

To count up by 5’s, use:

for num in range(0, 21, 5)

We can even count DOWN from a higher number to a lower one. The step value just needs to be negative:

for num in range(50, 39, -1)   # Counts from 50 down to 40

Note

For range(), the start and step values are OPTIONAL.

7.3.3. Try It!

Change the values inside of range to accomplish the following:

  1. Print the numbers 0 - 5.

  2. Print the numbers 33 - 45, including 45.

  3. Print only the odd numbers from 0 - 20.

  4. Print the numbers 25, 35, 45…95.

  5. Print the numbers from -3 to -10.

  6. Print by 3’s from 15 to -21.

1
2
3
4
5
# Change the start, stop, and step values in range to solve tasks 1 - 6 in the text.
# Tip: Use the 'clear' button in the console to remove old outputs.

for num in range(8):
   print(num)

7.3.4. Use Variables in range

To make a for loop run, we must tell Python exactly how many times we want the loop body to repeat. However, sometimes this number changes each time the program runs. Variables to the rescue! Whenever possible, use variables instead of specific numbers inside range().

Try It

Let’s try printing a for loop using the following variables to set the range.

start_value = int(input("Enter the FIRST number to print: "))
stop_value = int(input("Enter the LAST number to print: "))
step_value = int(input("Enter the step value for the loop: "))

Warning

A common mistake for new coders is to forget that the stop value in range is NOT assigned to the loop variable at any time.

After you pasted in the input statements and ran the program, did you have to type 0, 6, 1 to get the numbers 0 - 5 to show in the console? The input statement implies that we want our typed stop value to show up, but using the variable in range skips that number.

How do we fix this?

7.3.5. Use Expressions in range

Not only can we use variables inside range, we can also use expressions, which we practiced in the Data and Variables chapter.

Try It!

for num in range(start_value, stop_value+1, step_value):

The stop_value above, will add 1 to the inital stop_value. By adding to your stop_value, will increase loops that have a positive, or increasing, step_value.

With the negative stop_values, or decreasing step_values, adding to your stop_value may actually shorten the length of your final loop.

In order to extend a negative range, try reducing your stop_value. By reducing, or subtracting, from your stop_value you will extend loops involving negative numbers or negative step_values. In turn, this will shorten loops between positive numbers, or with positive step_values.

Try It!

for num in range(start_value, stop_value-1, step_value):

Expressions are not limited to standard arithmetic operators.

Try It

Run the following program. Enter different words to see how the behavior changes.

1
2
3
4
word = "Python"

for num in range(len(word)):
   print(word*num)

When Python executes the for statement, the expression len(word) returns the length of the string. So if word = "Hi", then range(len(word)) acts just like range(2).

7.3.6. Check Your Understanding

Question

In the command range(3, 10, 2), the second argument (10) specifies that range should:

  1. generate a set of values that stops at 9 (including 9).

  2. generate a set of values that starts at 10 (including 10).

  3. generate a set of values starting at 3 that stops at 10 (including 10).

  4. generate a set of values using every 10th number between 3 and 10.

Question

What command correctly generates the values 2, 5, 8 in that order?

  1. range(2, 5, 8)

  2. range(2, 8, 3)

  3. range(2, 10, 3)

  4. range(8, 1, -3)

Question

What happens if you give range only one argument, like range(14)?

  1. It will generate a set of values starting at 1 and ending with the number in the ().

  2. It will generate a set of values starting at 1 up to but NOT including the number in the ().

  3. It will generate a set of values starting at 0 and ending with the number in the ().

  4. It will generate a set of values starting at 0 up to but NOT including the number in the ().