Python | range constructor
Start your free 7-days trial now!
Python's range(~) constructor returns an immutable sequence with a series of numbers within a given range.
Parameters
1. startlink | int | optional
The start integer for the sequence (inclusive). Default 0.
2. stop | int
The stop integer for the sequence (non-inclusive).
3. steplink | int | optional
Increment for each integer in the sequence. Defaults to 1.
Return value
An immutable sequence with a series of numbers within a given range.
Examples
Basic usage
To return a range object that begins at 0 and ends at 5 (non inclusive):
range(5)
range(0, 5)
Start parameter
To create a list starting from 1 and ending at 5 (non inclusive) from a range object:
[1, 2, 3, 4]
Note that we return a list that starts at 1 and stops at 5 (non inclusive).
Step parameter
To specify a step of 2 for the sequence:
for i in range(1, 10, 2):
13579
From start value of 1, we increment by 2 until we reach the stop value of 10.