Syntax of range

1 min read ·

The range() function supports three different syntactic forms.
Each form controls how the numeric sequence is generated.

range(stop)

This is the simplest form of range().
It generates numbers starting from zero and stops before the given value.
Output values are 0 1 2 3 4
The stop value is not included in the sequence.

range(start, stop)

This form allows specifying a custom starting value.
The sequence begins at the start value and ends before the stop value.
Output values are 2 3 4 5
The stop value is always excluded.

range(start, stop, step)

This form allows controlling the gap between numbers.
The step value determines how much the number increases on each iteration.
Output values are 1 3 5 7 9
The step value must not be zero.
Caution

Using a step value of zero causes a runtime error.

The choice of syntax depends on how much control is required over the generated sequence.