Python range() Function – reverse, increment, decrement

Python range() Function – reverse, increment, decrement

The python range() function is the core in-built function to used perform an action until the specified number of times. So in this tutorial, we will learn all about the python range() function. We will learn how you can use it in python, how you can use it to loop function, how you can make a reverse order range loop by specified increment or decrement?

So let’s get started.

As I said python range() function is a built-in function and it is a type of python sequence. It is used when you want to perform an action a specific number of times. It will return the sequence of numbers starting from 0 and increment by 1 and stop before the specified number.

Syntax:
range(start, stop, step)

In python 3, the range() function is the updated name of xrange() function in python 2. Basically, the range() function in python 3 is renamed version of xrange() function in python 2.

Both range() and xrange() functions to deal with the numbers to iterate with for loop and generate the sequence of numbers.

So range() function is used for looping the sequence of python (List, String) with for loop and while loop. It will iterate the sequence of the list, starting from 0 indexes, and run until the stop specified number. You can also add a step to the python range() function to increment and decrement.

So let’s get into it and work with the range() function and see some examples of the range() function in python with reverse order loop, increment, decrement, etc.

Python range() Function Basics

In basic words, the range() function allows you to create a sequence of numbers inside a specified range. You can specify where the sequence of numbers begins and ends, as well as how large the step is between the numbers is. It is based on how many parameters you give to the function.

As you see in the syntax above, the range() function accept the three-parameter are:

  • start: An integer number specifying at which position to start. Default is 0
  • stop: An integer number specifying at which position to stop (not included).
  • step: An integer number specifying the incrementation. Default is 1

The start and step parameters are optional but the stop parameter is required and you have to specify this in the range function.

Let’s see some examples of basic range() function.

Example 1:
Output:
0
1
2
3
4
Example 2:
Output:
10
20
30
40
50
Example 3:
Output:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25

Lets’s see the three different ways of range() function that you can use in python to make increment, decrement or reverse the sequence using the specified arguments:

  • range(stop) takes one argument.
  • range(start, stop) takes two arguments.
  • range(start, stop, step) takes three arguments.

range(stop)

When you call range() function with just one argument is that stop, you’ll get a list of numbers that starts from 0 and the stop before specifying the stop number as an argument.

It will be the simple range function to print the sequence of numbers. See the image below and the code snippet.

Python range(stop)

Output:
0
1
2
3
4
5

range(start, stop)

With the two arguments (start and stop) in the range() function, you can specify the stop number of the sequence and can also tell to function where should start the sequence.

So, it’s not compulsory to sequence start from 0 all the time, you can specify your starting number and generate a sequence of numbers.

Let’s see the example of two arguments in range() function.

Python range(start, stop)

Output:
11
12
13
14
15

range(start, stop, step)

When you call the range() function with all three arguments, you can specify the starting number of the sequence, you can specify where should it stop, and what will be the step between numbers.

If you don’t provide a step argument, then the range() function will automatically behave as if the step is 1.

Note: The step arguments can be a positive or negative number, but it cannot be a 0.

See the example below with three arguments.

Python range(start, stop, step)

Example 1:
Output:
1
4
7
10
13
Example 2:
Output:
Traceback (most recent call last):
  File "", line 1, in 
ValueError: range() arg 3 must not be zero

Increment With range() Function

The increment means, the process of increasing the number one by one in any sequence. So in the range() function, you have to add a step argument to increment.

Suppose you want to increment in your range() function then you need to add step argument as a positive number. Must be the positive number for increment. If you will add a negative number then it works as a decrement, we will see it later.

Let’s have a quick example of range() increment.

Output:
0
25
50
75
100

In the above example, you see, we added 25 as a step argument to increment by 25 each time. So it will start from 0 and increment by 25 and print the all number sequence that makes the condition true.

Decrement With range() Function

As you see in the above example we added a positive number to increment the number of sequences. So, if you want to decrement your sequence then you need to add step argument as a negative number.

So it will print the sequence number from last to start by decrementing with your provided step in a negative number.

See the example below.

Output:
100
75
50
25
0

In the above example, we added the negative number -25 as a step and starting number is 100 and the stop number is -1. So it will return the sequence of numbers in reverse order by specified decrementing numbers.

But do yo know that python have built-in reversed() function and you can also do same thing with that.

Python reversed() Function

The python reversed() function you can use to make reverse order of sequence such as list and range(). So when you wrap the range() function inside the reversed() function than the number of sequence returns in reverse order.

We will use the same above example for increment but we will just wrap the range() in reversed() function. Let’s see in the example.

Example 1:
Output:
0
25
50
75
100
Example 2:
Output:
14
13
12
11
10

Reverse a List with range() Function

You can also use the range() function to make your list in reverse order. Just pass the count of list items as a start number and use negative number -1 as a step, and the stop number will be in reverse order for how many items you want from the list.

Output:
100
75
50
25
5

Float Numbers with range() Function

You may have noticed that all of the numbers we have been using with the range() function are integers. That’s because range() can take only integers as arguments.

In Python, if numbers are not integers numbers, then those are float numbers. Float number have pointing the decimal in the numbers. It can be positive or negative.

So, if you will try float number with range() function then you will get the error message. Let’s see the following example.

Output:
Traceback (most recent call last):
  File "", line 7, in 
TypeError: 'float' object cannot be interpreted as an integer

But if you really want to work with float numbers then you can use the NumPy third-party python library. So, let’s have a look at an example with NumPy.

Float Number With range() Using NumPy

First thing you need to install NumPy library with pip command and import it on your page.

pip install numpy
import numpy

Then use the following example to print a number of sequences with float numbers using the NumPy library.

Output:
0.2
0.4
0.6000000000000001
0.8
1.0

Convert range() to list

Python range() function return immutable sequence of numbers and it is not a type of list. But we can convert range() to list with the help of list() constructor.

Let’s see how we can convert range() to list.

Output:
5
10
15
20
25

So in the above example, we used the list() constructor to convert the range() to list. Then made the normal for loop and print the item on the list. Thus we have created the list using the range function.

Hope you understand all the guide about python range() function and how you can reverse, increment, decrement with range() function.

If you still have any queries please let me know in the comment section, I’ll respond to you as soon as possible.

More Python Tutorials

>> How to Create Nested Functions in Python?
>> Multiple Constructors For Class in Python
>> How to Test Internet Speed Using Python?

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published.