The range builtin function in Python

Article updated on Saturday, March 16, 2024.

Python for i in range

Python’s built-in range function is handy when you need to perform an action a certain number of times. As an experienced Pythonist, you have probably already used it. But what does it do exactly?

In Python, the native function range() returns a list of numbers taking from 1 to 3 integers: start, stop and step. Range is often used in simple for balls for i in range when you want to iterate an action a certain number of times.

The 3 parameters the range function can receive are:

  • start - an integer that determines the first element in the sequence of numbers that will be returned
  • stop - the integer up to which we will loop but not including (as we start counting from 0)
  • step` - an integer that can be positive or negative to define how much to increment between each element in the list

With a for loop in python, we just need an iterable to loop on. And that’s good, that’s what the range function returns !

Loops for i in range in Python

Loop for i in range with 1 argument

Calling range with only one parameter, we give it a stop. Thus, range(3) returns: [0, 1, 2].

In a for loop, this gives:

for i in range(3):
  print(i)

Which will display on the screen …

0
1
2

Loop for i in range with 2 arguments

Calling range with two parameters, we give it a start and a stop. Thus, range(2, 8) returns: [2, 3, 4, 5, 6, 7].

In a for loop, this gives:

for i in range(2, 8):
  print(i)

Which will display on the screen …

2
3
4
5
6
7

If the second argument is smaller than the first, range returns an empty list, so we won’t enter the loop.

print(list(range(4, 0)))

Returns [] on standard output.

Loop for i in range with 3 arguments

And finally calling range with three parameters, we give it a start, a stop and a step. So range(3, 10, 2) returns: [3, 5, 7, 9].

But we can also have fun by giving a negative step, which in a for loop would give:

for i in range(0, 42, 7):
  print(i)

Which will display on the screen …

0
7
14
21
28
35

And for the clever ones who want to create an infinite loop with range(42, 0, 7) for example, know that Python will not enter the loop as start is greater than stop (0) and step is positive.

Because yes, step or the other parameters of the range function can be negative numbers 🤯

Iterating backwards with range in Python

We can iterate backwards with range in Python by giving a negative number as the 3rd argument in the function.

For example, if we want to count backwards from 10 to 1, we would have:

for i in range(10, 0, -1):
  print(i)

Let’s have a look at what this range is made of:

  • 10 is the number we start with, included
  • 0 is our end number, not included
  • -1 is the jump we’ll make from our start number to our end number (not included)

Our 3rd argument which we called step or jump in this article can also be less than -1. So we can go from -7 to -7, from 42 to 0 (not included):

for i in range(42, 0, -7):
  print(i)

This will give, if you have followed …

42
35
28
21
14
7

Index range in Python

Here we go down into the abyss of Python and you should normally never need to do something like this. But just for your information, you should know that you can index and slice a range object in Python.

In Python, the range function returns an object of type range. Since an object of type range is an iterable (like a list), we can also index or slice it.

my_range_object = range(0, 4) # 0 1 2 3

indexation = my_object_range[2]

print(indexing) # 2

In this example, the variable indexation will be 2 because my_object_range is similar to a list [0, 1, 2, 3] and the element in position 2 of this list is the number 2.

We can also split the range object:

my_range_object = range(0, 4) # 0 1 2 3

print(my_range_object[:2]) # 0 1
print(my_object_range[2:]) # 2 3
print(my_object_range[1:2:3]) # 1

Go further in your learning of Python!

Python proves once again to be a robust language while being flexible and fun. The range function has now no secrets for you and you have also learned how to master for loops using range!


Thomas Collart

Hey, I'm Thomas 👋 Here, you'll find articles about tech, and what I think about the world. I've been coding for 20+ years, built many projects including Startups. Check my About/Start here page to know more :).