Python Range with Step

Python Range with Step

We can create a Range object with sequence of numbers where the adjacent numbers are separated by a step value.

Python Range with start, stop, and step

Syntax

The syntax of range() function with the step parameter is

range(start, stop[, step])

where start and stop are the starting of the sequence and the ending of the sequence (stop not included), and the items are defined in given steps from start value.

Examples

1. Range from 2 to 15 in steps of 3

In this example, we shall iterate over a range that starts at 2 and goes until 15 in steps of 3.

Python Program

for r in range(2, 15, 3):
    print(r)
Run Code Copy

Output

2
5
8
11
14

1. Range from 2 to 15 in steps of 4

In this example, we shall iterate over a range that starts at 2 and goes until 15 in steps of 4.

Python Program

for x in range(2, 15, 4):
    print(x)
Run Code Copy

Output

2
6
10
14

Summary

In this tutorial of Python Examples, we learned how to create a range/sequence that has a step value between adjacent elements.

Code copied to clipboard successfully 👍