range() Function in Python

By Hemanta Sundaray on 2021-09-09

The range() function returns a stream of numbers within a specified range without us having to first create and store a large data structure such as a list or tuple. This allows us to create huge ranges without using all the memory in the computer.

Syntax

range(start, stop, step)
  • If we omit start, the range begins at 0.

  • The only required value is stop. The last value created will be the value just before stop.

  • The default value of step is 1. We can go backwards with -1.

range() returns an iterable object. We can convert this iterable object to a sequence, like a list, using the list() function.

for num in range(5):
    print(num, end=" ")

# output
# 0 1 2 3 4
sequence = list(range(1, 10, 2))

print(sequence)
# [1, 3, 5, 7, 9]
Learn about the end keyword argument in the print() function in my blog post here.

Join the Newsletter