Python range() Function

Artturi Jalli
5 min readSep 6, 2021

Python range function returns a sequence of values you can loop over.

For example, let’s print values from 0 to 5 using the range() function in a for loop.

for number in range(0,6):
print(number)

Output:

0
1
2
3
4
5

You can also take bigger steps with the range() function. To do this, provide the range() function with a third argument that is the stepsize.

For example, let’s print values from 0 to 50 by taking steps of 10:

for number in range(0, 60, 10):
print(number)

Output:

0
10
20
30
40
50

To create a range in descending order, you can provide range() function with negative step size as a third parameter.

For example:

for number in range(5,0, -1):
print(number)

Output:

5
4
3
2
1

Here you saw some common ways to use the range() function in a for loop. But it’s also worthwhile to understand how the range() function really works.

Today you are going to see the three ways you can call the range() function. You will also see some use case examples. Finally, you will learn when you should not use the range() function.

Let’s jump into it.

Python range() Function in Detail

The built-in range() function in Python is used to create sequences of numbers. The most common use case for the range() function is to use it in conjunction with a for loop:

for number in range(some_number):
# Do something

But before we use the range() function in a for loop, let’s understand how the range() function works.

Python’s built-in range() function can be defined in the following ways:

1. range(end)
2. range(start, end)
3. range(start, end, stepsize)

Let’s go through each approach in more detail.

1. range(end)—Only Specify the End Value for a Range

This is the most basic way to use the range() function in Python.

This syntax creates a range that starts from 0, and ends one value before the end.

For instance, let’s create a range from 0 to 5:

nums = range(6)

Now nums is a range object. If you try to print it out, you get:

range(0, 6)

If your goal was to create a list of numbers, you can convert the range object to a list object. To do this, use the built-in list() function.

nums = list(range(6))
print(nums)

Output:

[0, 1, 2, 3, 4, 5]

2. range(start, end)—Specify Both Start and End Values for a Range

Another way to create a range is by specifying both start and end values.

What may initially be confusing, is that the start value needs to be the desired start value. But the end values must be one plus the desired end value.

For instance, let’s create a range from -2 to 2. In this case, you need to set the start as -2, but the end as 3.

range(-2,3)

If you want to create a list of numbers between the range, you can use the built-in list() function.

For instance:

rng = range(-2, 3)
nums_list = list(rng)
print(nums_list)

Output:

[-2, -1, 0, 1, 2]

3. range(start, end, stepsize)—Range with a Custom Step Size

You don’t always want to create a range where consecutive numbers are separated by one. This is where you can use the range() function with a third argument, the stepsize.

For instance, let’s create a list of numbers from -20 to 20 by taking steps of size 10. To do this, let’s create a range, where we specify the start point at -20. To cut it at 20, the endpoint needs to be 30 with the step size of10.

rng = range(-20, 30, 10)
nums_list = list(rng)
print(nums_list)

Output:

[-20, -10, 0, 10, 20]

Python range() Function Usage Examples

Use range() with a For Loop

The range() function is commonly used in conjunction with a for loop.

For instance, let’s print numbers from 0 to 5 using for loop and the range() function:

for number in range(0, 6):
print(number)

Output:

0
1
2
3
4
5

Increment with Python’s range() Function

As you already saw, you can create a range of values in increasing order.

For example:

for num in range(0, 4):
print(num)

Output:

0
1
2
3

Decrement with Python’s range() Function

Nothing prevents you from going in the opposite direction with the range() function.

To do this, you need to specify a negative step size for your range() function.

For example:

for num in range(4, 0, -1):
print(num)

Output:

4
3
2
1

If you want to create a range in decreasing order, you can do it with the range backward. But you can also create an increasing range and reverse it.

To reverse the range, call the built-in reversed() function on it.

For example, let’s print numbers from 4 to 1 again:

for num in reversed(range(1, 5)):
print(num)

Output:

4
3
2
1

Ignore the Index When Looping

Sometimes you may want to repeat an action n times without caring about the index.

You can use the range() function, for example:

for i in range(5):
print("Hello!")

Output:

Hello!
Hello!
Hello!
Hello!
Hello!

But as you don’t need the index i, you can ignore it using the _ notation.

For example, let’s repeat the above loop but neglect the index i:

for _ in range(5):
print("Hello!")

Output:

Hello!
Hello!
Hello!
Hello!
Hello!

Do Not Use range() Function Blindly

Now you’ve learned what the range() function is and how to use it.

Let’s see an example when using a range() function may not be the best option. In this example, there is a line in a store, where there are three customers. Your task is to print the line such that each customers’ name and position is displayed.

As you’ve just learned about the range() function, your initial solution is probably going to look like this:

queue = ["Jack", "Ann", "Sofie"]for i in range(len(queue)):
name = queue[i]
print(f"{name} is at {i+1}. position")

Output:

Jack is at 1. position
Ann is at 2. position
Sofie is at 3. position

Here you create a range based on the length of the list to keep track of the index. Even though this solution works, it is not the most elegant in this case.

In Python, there is a built-in function called enumerate(). Its job is to keep track of the current element and the index. It works by creating a (index, item) pairs based on the collection.

Let’s use enumerate() in the above example in place of range():

queue = ["Jack", "Ann", "Sofie"]

for pos, name in enumerate(queue):
print(f"{name} is at {pos + 1}. position")

Output:

Jack is at 1. position
Ann is at 2. position
Sofie is at 3. position

Conclusion

Today you learned about the Python’s built-inrange() function.

You should now have the knowledge to create sequences of:

  • Numbers in increasing order.
  • Numbers in decreasing order.
  • Numbers with a custom step size.

You also know that the range() function is useful in for loops. But you should still not use it blindly, when better alternatives, such as enumerate(), exist.

Thanks for reading.

Happy coding!

Further Reading

References

--

--

Artturi Jalli

Youtube: @jalliartturi to take your blog to the next level