howtouselinux

5 Useful Examples to use Python 3 Range Function

Table of Contents
 

Python range() is a built-in function available with Python from Python(3.x), and it gives a sequence of numbers based on the start and stop index given. 

In case the start index is not given, the index is considered as 0, and it will increment the value till the stop index.

Python 3 Range function Syntax

range(start, stop, step)

Parameters

  • start: (optional) The start index is an integer, and if not given, the default value is 0.
  • stop: The stop index decides the value at which the range function has to stop. It is a mandatory input to range function. The last value will be always 1 less than the stop value.
  • step: (optional).The step value is the number by which the next number is range has to be incremented, by default, it is 1.
 
More details for this:
 
  • All parameters must be integers (positive or negative).
  • start is inclusive number. It means that start will be part of the sequence and will be the first number in the sequence.
  • stop is exclusive number. It means that stop will NOT be part of the sequence. The sequence will terminate exactly before the stop number.
  • Sequence obtained from range() follows zero-base indexes. It means that in a sequence of n numbers, indexes will start from 0 and end at n-1.
  • step is optional parameter. If step is zero, ValueError is raised.

Python range() function with only stop

When we give only one argument to range(), the argument is considered as stop.

The default value of start is 0 and step is 1.

Example Program
Let us write an example Python program with range(stop).

				
					
for r in range(5):
    print(r)
				
			

Run the above program, and the contents of the range(5) are printed to the console one after another in a new line.

0
1
2
3
4

Python range() function with start and stop

When we provide two arguments to range(), the first is start and the second is stop.

The default value of step is 1, and therefore the contents of range has elements starting from start, in steps of 1 until stop.

Example Program
Let us write a Python program, with range() accepting start and stop.

 

				
					
for r in range(4, 9):
    print(r)
				
			

Run the above program and you should get the range with contents starting at 4 incrementing in steps of 1 until 9.

4
5
6
7
8

Python range() function with start and stop and step

In this example, we shall iterate over a range, with positive step, and during each iteration, let us print the range element.

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

Output

When we run the above program, we should get the elements that we computed at the start of this section.

2
4
6

Python range() function with negative step

In this example, we shall iterate over a range, with negative step, and during each iteration, let us print the range element.

				
					
for r in range(8, 2, -2):
    print(r)

				
			

When we run the above program, we should get the elements that we computed at the start of this section.

8
6
4

Initialize Python List with range function

We can initialize a Python List using a range.

In the following Python Program, we shall initialize a Python List with numbers in the range(4, 9).

				
					
x = list(range(4, 9))
print(x)
				
			

list() constructor can take a range as argument and create a list from these range elements. When you run the above program, you shall get the list printed to the console, as shown below.

[4, 5, 6, 7, 8]

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press