Range in Python

What is range?

The range() function generates a sequence of numbers, commonly used for looping a specific number of times.

Syntax

range(start, stop, step)

Examples

for i in range(5):
    print(i)
# Output: 0 1 2 3 4
  
for i in range(2, 7):
    print(i)
# Output: 2 3 4 5 6
  
for i in range(1, 10, 2):
    print(i)
# Output: 1 3 5 7 9
  
list(range(4))
# Output: [0, 1, 2, 3]
  
list(range(10, 5, -1))
# Output: [10, 9, 8, 7, 6]