Range Function

Carson West

For Loop Examples

Range Function

The range() function in Python is a built-in function that generates a sequence of numbers. It’s commonly used in loops to iterate a specific number of times.

Syntax:

range(stop)
range(start, stop[, step]])

Examples:

# Basic usage:
for i in range(5):  # iterates 0, 1, 2, 3, 4
    print(i)

# Specifying start and stop:
for i in range(2, 7): # iterates 2, 3, 4, 5, 6
    print(i)

# Specifying start, stop, and step:
for i in range(1, 10, 2): # iterates 1, 3, 5, 7, 9
    print(i)

# Negative step:
for i in range(10, 0, -1): # iterates 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
    print(i)

#Creating a list from range:
my_list = list(range(1,6)) # Creates 1, 2, 3, 4, 5
print(my_list)

Important Considerations:

Range Object (Iteration in Python)