Register Login

Python range() Function Explained with Examples

Updated Apr 23, 2020

What is Python range() Function?

In python, we use range() function when we have to generate a sequence of numbers in a given range. It is a built-in function.

We generally use range() function with for and while loop to generate a sequence of numbers.

Suppose you want to print a specific sequence of numbers such as 1,2,3,4 and 5. You can type in five print() statements such as:

print(1)

print(2)

print(3)

print(4)

print(5)

In case you have to print out many numbers, this may be a time-consuming and troublesome process. So, Python provides you with a range() method for easily generating a sequence of numbers. You can use this along with a for or while loop to print the results.

Syntax:

range(start, stop, step)

Parameter

  • start: (Optional): integer number to specify starting position, Default: 0
  • stop: (Required): Integer number to specify starting position 
  • step: (Optional) : Integer number to specify increment: Default: 1

1) range() with Stop Parameter

Example

# Program to explain range() function
# range() function with single parameter

# for loop to print number from 0 to 4
for i in range(5):
  print(i)

Output

0
1
2
3
4

Explanation

This is a program to print out the numbers from 0 to 4. A for loop is used along with the range() method in this case. A variable i is used to loop through the numbers in the range. In the line range(5), there is no other parameter. So, the range() method treats this as the stop parameter. As a result, the output is 0,1,2,3, 4. So, as the stop parameter is 5, the range() method stops printing at 4. Moreover, as there is no start parameter provided, range() considers 0 as the default parameter.     

2) range() with Start and Stop Parameter

Example

# Program to explain range() function
# range() function with start and stop parameter

# for loop to print number from 1 to 5
# 1 as start argument and 6 as stop argument
for i in range(1, 6):
  print(i)

Output

1
2
3
4
5

Explanation

In this code, a for loop is used to print out the numbers 1 to 5 with the help of a range() method.  Two arguments are passed into the range() method – 1 and 6. Python considers the first argument 1 as the start parameter and the second argument 5, as the stop parameter. So, the final result will be 1,2,3,4, 5. The variable i is used for iterating through the range of numbers.

As no step argument is passed, Python considers 1 as the default step parameter.  Hence the numbers are printed from 1 to 5 with a step difference of 1.

3) range() with Start, Stop and Step Arguments

Example

# Program to explain range() function
# range() function with start and stop parameter

# for loop to print number from 1 to 9 with increment of 2
for i in range(1, 10, 2):
  print(i)

Output

1
3
5
7
9

Explanation

The code prints out the numbers from 1 to 9 using the range() method. A variable i is used for iterating through the loop. There are three parameters for the range() method. The start argument is given as 1, the stop argument is mentioned as 10 and 2 is the step parameter.

So, the final result is 1,3,5,7,9.

4) range() function with Decremented or Reverse Step

Example

# Program to explain range() function
# range() function with start, stop & step parameter
# negative value in step parameter to print numbers in decremented manner

# for loop to print number from 10 to 2 with Deterrent of 2
for i in range(10, 1, -2):
  print(i)

Output

10
8
6
4
2

Explanation

In this code, the numbers are printed out in reverse order. The numbers 1 to 10 are printed using the range() method, that has three arguments – start, stop and step. The range starts at 10, stops at 1 and has a step decrement of -2. So the resultant output is 10,8,6,4,2.  

Error: TypeError: 'float' object cannot be interpreted as an integer

When we give float number with range() function as range() function doesn't support the float numbers and raised the above error.

Example

# Program to explain range() function
# range() function with float parameter

for i in range(2.6):
  print(i)

Output

Traceback (most recent call last):
  File "rrange.py", line 5, in <module>
    for i in range(2.6):
TypeError: 'float' object cannot be interpreted as an integer

Explanation

In the code, we can see that there is a for loop with a range() method. A variable i is used for iteration within a range value of 2.6. As no other parameters are provided, the range() method considers this as the stop parameter.

Here, an error called “TypeError: 'float' object cannot be interpreted as an integer” after the execution of the program. This is because the range() method only accepts an integer value as a parameter. As 2.6 is a float value, it raises the error.

Note:

1. range() function takes integer only.
2. All three parameters can be positive or negative.
3. step parameter can not be zero.
4. range() built-in function is exclusive in nature, it doesn't include the last value specified in “end” parameter.

Conclusion

When using the range() method, make sure that the parameters are passed properly. Avoid the use of any float values for the parameters.  


×