Skip to main content

range()

The range() function in Python is a built-in function that generates a sequence of numbers. It can take up to three arguments - start, stop, and step. By default, it starts at 0 and increments by 1. It is commonly used in for loops to iterate over a sequence of numbers.

Parameter Values

Parameter Description
start

The starting value of the sequence.

stop

The end value of the sequence (exclusive).

step

The increment between each value in the sequence (default is 1).

Return Values

The range() function in Python returns a range object.

How to Use range() in Python

Example 1:

Return a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

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

Specify the starting point, ending point, and step size for the sequence.

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

Create a descending sequence by specifying a negative step size.

for i in range(10, 0, -1):
    print(i)