Python Tutorial Python Advanced Python References Python Libraries

Python range() Function



The Python range() function returns a sequence. By default, sequence starts from 0 and ends at specified integer with increment of 1. Range() function has two optional arguments, which can be used to define starting point and increment / decrement factor of the sequence.

Syntax

range(start, stop, step_size)

Parameters

start Optional. An integer. starting point of sequence. default is 0.
stop Required. An integer. end point of sequence.
step_size Optional. An integer. step size of sequence. default is 1.

Example:

The example below describes how to use range() function to return a sequence.

MyRange = range(4)
for i in MyRange:
  print(i)
print()

MyRange = range(10, 14)
for i in MyRange:
  print(i)
print()

MyRange = range(-2, -10, -2)
for i in MyRange:
  print(i)

The output of the above code will be:

0
1
2
3

10
11
12
13

-2
-4
-6
-8

❮ Python Built-in Functions