How to use the range function in Python

In Python, the range() function is used to generate a sequence of integers, where you can specify the start, stop, and step values using one, two, or three parameters.

Option:

Usage 1: range(stop)

  1. Generate a sequence of integers from 0 to stop-1, with a default step size of 1.

Option 1:
Usage 2: range(start, stop)

  1. Generate a sequence of integers from start to stop-1, with a default step size of 1.

Usage three: range(start, stop, step)

  1. Generate a sequence of integers starting at “start” up to “stop-1” with a step size of “step”.

Example 1: The rules of the game are clear.

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Example 2:

for i in range(2, 8):
    print(i)

Output:

2
3
4
5
6
7

Example 3:

for i in range(1, 10, 2):
    print(i)

Output:

1
3
5
7
9
bannerAds