Data / Data Science / Software Development

Python for Beginners: The Range() Function

15 Feb 2022 8:00am, by
Python language logo on blue background

Welcome back to Python for Beginners! In this series, we’re breaking down Python into very small, digestible chunks so anyone (of any skill level) can learn how to program with this user-friendly, versatile language. So far we’ve covered functions, lists, if/else statements, saving input to a file, and and/or operators. We’ve put these pieces together to help you see how applications are created and how to build functioning tools such as calculators.

This time around, we’re going to talk about the range() function. Although you might not see the value of this function at first, eventually you’ll find it to be a very helpful tool.

What Is the range() function?

Simply put, the range() function makes it possible to generate a sequence of numbers between a given range. For example, you might want to print out (or use) a range of numbers from 0-9. Depending on how arguments are passed through the function, a user can determine where the series of numbers begins and ends.

The range() function accepts three arguments:

  • start – integer where the range starts.
  • stop – integer before the sequence is to end.
  • step – how to increment between each integer in the sequence.

We’ll obviously start off with something simple, so you can easily understand the concept of the range() function.

The Basics of range()

Let’s first use the Python console to demonstrate how range works. Log in to the console with the command:

python3

Type the following command:

list(range(1, 10))

You should see the following output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Wait, wasn’t our range from 1 to 10? Where’s the 10? That’s where it gets a bit tricky. You see 1 is our start but the very definition of stop is the integer before the sequence is to end. So if you wanted to print out a range from 1-10 (including the 10), the command would be:

list(range(1, 11))

The above command would output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Let’s add a for loop into the mix to print a range from start to end. To do this, we’re going to add the end= parameter that will instruct Python what to do after each step in the range. This will make sense when you see the output. Still in the console, type:

for i in range(20):

Hit enter on your keyboard and then type (include the spaces before print):

    print(i, end= " ")

Hit enter twice and you’ll see the output:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

By using end= ” ” we tell Python to put a space between each number. If, however, our end statement was:

print(i, end= "\n")

The output would be:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Why is that? Because we added a newline sequence (\n) as the end parameter.

Let’s make this a bit more useful.

How to Use range() within Code

What we’re going to do first is allow the user to input our range. To do that we have to approach the inputting variables a bit differently than we have before. What we have to do is inform Python our input will be an integer. We’re also going to combine the text asking the user to input a number with the variable declaration.

Create the new file with:

nano range.py

The first thing we’ll do is ask the user to enter a number to serve as the start of our range. That line is:

x = int(input("Type a number to start the range:"))

The line which asks the user to input a number to end the range is:

y = int(input("Type a number to end the range:"))

Next, we’ll use a for loop to print the range from the user’s input. That statement looks like this:

for i in range(x, y):

print(i)

So our entire program looks like:

Save and close the file. Run the program with:

python3 range.py

When prompted, enter the start and end points and the program will print out the range.

Simple.

How to Use the Step Option

Let’s get a bit more creative and make use of the step option. What this does is allow you to define the increments, by step number, for a range. So instead of printing every number in the range from 1-20, we can tell Python to only print every other number.

Let’s do just that. We’ll go back to using the console. Log in to the Python console with:

python3

Remember our first command:

list(range(1, 10))

If we add a third number to that, we instruct Python on how to iterate the range. So if we issue the command:

list(range(1, 10, 2))

our output would be:

[1, 3, 5, 7, 9]

If we wanted even numbers, we’d have to start our range with 0, like this:

list(range(0, 10, 2))

Pretty simple.

You can also use a range that prints in reverse order. For example, type:

list(range(10, -10, -1))

The output of the above command would be:

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

Or we could step the iteration by -2 with:

list(range(10, -10, -2))

The output of the above command would be:

[10, 8, 6, 4, 2, 0, -2, -4, -6, -8]

Remember, to exit out of the Python console, type:

exit()

Stepping with Decimal Points

Let’s get creative and make use of the NumPy library. With the help of NumPy and the arrange() function, we can use numbers with decimal points as both range and step. Create a new file with:

nano range1.py

The first line in our file will import the NumPy library as np:

import numpy as np

Next, we’ll create a for loop to print our range with:

So our full application looks like this:

Save and close the file and run it with:

python3 range2.py

The output for this program will be:

0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0

As you can see, we’ve stepped the range by .5 from start to end.

And there you go. You now know how to use the range() function in various ways with Python. Your range of skills with Python will continue to grow.