How to use Python range

With Python range you can create individual number sequences. The function is very practical, especially when used together with Python for loops.

When is Python range used?

In essence, the range function is used to create a number sequence that follows a set pattern. Since the function is primarily used together with for loops, it’s common to find it in almost every Python tutorial. By using Python range, you can easily specify the area that you wish to execute a code block in with just one function call.

Tip

Python is the perfect tool for web projects. If you want to start your own web-based project, using Deploy Now from IONOS is a good idea. Automatically deploy all your updates via GitHub and keep an eye on your project in real time.

What is the syntax for Python range?

The syntax of Python range is fairly simple. The function can use up to three parameters and gives a number sequence as the result.

range(start, stop, step)
python

While you don’t need to use all the Python range parameters, the stop parameter is required at all times. This is because it specifies the end value. Make sure that the value you enter is not included in the returned number sequence.

for i in range(6):
print(i)
python

In this example, the result is ‘0, 1, 2, 3, 4, 5’.

The start parameter is optional and specifies where the counting should begin. If you don’t enter a start parameter, the number sequence will begin with 0 as shown in the example above. If you want to start from three, for example, you can change your code as follows:

for i in range(3, 6):
print(i)
python

Now you should see the numbers ‘3, 4, 5’ displayed on the screen. In addition to the start and stop parameters, there’s also the ‘step’ parameter. This parameter lets you specify the interval between each number in the sequence more precisely. You don’t have to include the step parameter when using the Python range function in your code. If you don’t specify a value for the step parameter, it will default to 1. Let’s say, for example, you want to display every third number on your screen. The code below will show you how you can do this with the step parameter.

 for i in range(2, 10, 3):
print(i)
python

Beginning with the number 2, which is used as the starting parameter, every third number up until 10 will be output. So, the sequence of numbers returned is ‘2, 5, 8’.

Python range example: find all equal numbers below 100

By using the Python range function, you can easily output certain mathematical sequences. One example of this is outputting a sequence of even numbers. Here we can begin with the number 0, which as mentioned above is the default value of the start parameter. To output all even numbers less than 100, you can set the stop parameter to the number 100 and the step parameter to 2. This means that only every second number starting from 0 will be counted.

for i in range(0, 100, 2):
print(i)
python

This simple line of code will now display all even numbers between 0 and 100.

How to create decreasing number sequences with Python range

The range function also allows you to define decreasing number sequences. To do this, you need to make sure that the stop value is higher than the start value. To indicate that you want to count down instead of up, you need to enter a negative value in the step parameter. Here’s an example:

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

This example will output the following number sequence: ‘10, 9, 8, 7, 6, 5, 4, 3, 2, 1’.

In order to provide you with the best online experience this website uses cookies. By using our website, you agree to our use of cookies. More Info.
Manage cookies