Online Tutorials & Training Materials | STechies.com
Register Login

Python range() Function with Explained Examples

|| 0

Python range() Function with Explained Examples
Stechies

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

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

Syntax:

range(start, stop, step)

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

range() with stop parameter

# 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

In the above example we have given single parameter so python will treat this single parameter as required stop parameter and as we haven't define the start and stop parameter here so it will take their default value which 0 and 1.

range() with start and stop parameter

# 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

In the above example we have given two parameters, range() function will take first argument as start and second argument as stop and as we have not define step argument here so it will take step as default (1).

range() with start, stop and step arguments

# 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

In the above example we have given three parameters, range() function will take first argument as start, second argument as stop and step argument increment the value by 2 in the loop.

Range() function with Decremented or reverse step

# 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

In the above example we have given step parameter to -2 value which will print the number in decremented manner in the loop.

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

Error: ValueError: range() arg 3 must not be zero

This error comes when we set step parameter as zero.

# Program to explain range() function
# range() function with step parameter as zero

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

Output:

Traceback (most recent call last):
  File "rrange.py", line 5, in <module>
    for i in range(1, 10,0):
ValueError: range() arg 3 must not be zero

Points:

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 .