6.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?

6.3.1. Set Start and End 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, end_value)`. The starting value is included in the counting for the loop, but the end value is NOT.

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

What should be the start and end values in `range` to print the following?

```HelloHello
HelloHelloHello
HelloHelloHelloHello
```

Cool, `range(start, stop)` allows us to count upwards from any number we want.

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

6.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, `range(start_value, end_value, step_value)`.

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.

6.3.3. Try It!¶

In the editor below, 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.

6.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!

Tip

Whenever possible, use variables instead of specific numbers inside `range()`.

Paste these statements into the editor above (before the loop), and use the variable names in `range`:

 ```1 2 3``` ```start_value = int(input("Enter the FIRST number to print: ")) end_value = int(input("Enter the LAST number to print: ")) step_value = int(input("Enter the step value for the loop: ")) ```

Repeat each of the items in the Try It section above. Enter the start, stop, and step values to print the desired output.

Warning

A common mistake for new coders is to forget that the end 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 end value to show up, but using the variable in `range` skips that number.

How do we fix this?

6.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.

For the program above, replace the `for` statement with this:

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

For tasks 1 - 4, the expression `end_value + 1` makes sure that the value we type will be included in the loop. With the negative stop values in tasks 5 and 6, we need to use `end_value - 1`.

Try It

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

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)`.

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 ().