Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python's range() function

Share
Copied to clipboard.
Series: Looping
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read 2 min. video Python 3.9—3.13

Let's talk about Python's range function.

Counting upwards in Python

How can you count from 1 to 10 in Python?

You could make a list of all those numbers and then loop over it:

>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> for n in numbers:
...     print(n)
...
1
2
3
4
5
6
7
8
9
10

But that could get pretty tedious. Imagine if we were working with 100 numbers... or 1,000 numbers!

Instead, we could use one of Python's built-in functions: the range function.

The range function accepts a start integer and a stop integer:

>>> for n in range(1, 11):
...     print(n)
...
1
2
3
4
5
6
7
8
9
10

The range function counts upward starting from that start number, and it stops just before that stop number. So we're stopping at 10 here instead of going all the way to 11.

You can also call range with just one argument:

>>> for n in range(5):
...     print(n)
...
0
1
2
3
4

When range is given one argument, it starts at 0, and it stops just before that argument.

So range can accept one argument (the stop value) where it starts at 0, and it stops just before that number. And range can also accept two arguments: a start value and a stop value. But range also accepts a third argument!

Using range with a step value

The range function can accept an optional third argument:

>>> for n in range(0, 51, 10):
...     print(n)
...

What do you think this might do? What's your guess?

That third argument is the step value, which defaults to 1.

So this loop will count starting at 0 and stopping just before 51, but we'll count with a step of 10:

>>> for n in range(0, 51, 10):
...     print(n)
...
0
10
20
30
40
50

Counting backwards in Python

What if we wanted to count down instead of counting up?

One way to count down is to give a negative step value:

>>> for n in range(10, 0, -1):
...     print(n)
...
10
9
8
7
6
5
4
3
2
1

By using a step value of -1, we're counting from a larger number to a smaller number: we're counting downward, one number at a time.

Note that even when going in reverse, we stop just before our stop value. We're not stopping at 0 above, but at 1.

The arguments range accepts are similar to slicing

You can think of the three arguments given to range as similar to the three values we can use when slicing a sequence in Python:

>>> message = "value exalt apprise esteem"
>>> message[6:19:2]
'eatapie'

These indices that we give are: the start, the stop, and the step indices.

Just like with a range function, we have a start value, a stop value, and a step value:

>>> list(range(6, 19, 2))
[6, 8, 10, 12, 14, 16, 18]

With both slicing and the range function, that stop value is not included. The start value is included, but the stop value is excluded.

Using range with for loops

Python's for loops are all about iterables.

But what if we just wanted to count while looping? For that, we can use Python's built-in range function.

The range function is great for performing an operation a specific number of times, or for counting upward or counting downward.

Series: Looping

Unlike, JavaScript, C, Java, and many other programming languages we don't have traditional C-style for loops. Our for loops in Python don't have indexes.

This small distinction makes for some big differences in the way we loop in Python.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
5 Keys to Python Success 🔑

Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!