start , stop and step
2 min read ·
The
range() function generates numbers based on three parameters called start, stop, and step.Each parameter controls a specific part of the sequence.
Default Values
When only one argument is provided, it is treated as the stop value.
The start value defaults to zero.
The step value defaults to one.
Generated values are
0
1
2
3
This is equivalent to using start as zero and step as one.
How Values Are Generated
range() begins at the start value.After generating a number, it adds the step value to produce the next number.
This process continues until the next value would reach or cross the stop value.
The stop value itself is never included.
The sequence starts at one and stops before seven.
How Step Affects the Sequence
The step value controls the difference between consecutive numbers.
A positive step creates an increasing sequence.
Generated values are
1
4
7
A larger step value results in fewer generated numbers.
Important
The direction of the sequence depends on the sign of the step value.
Understanding start, stop, and step is essential for using
range() correctly in loops and index based operations.