The range function in Python is a versatile tool used to generate a sequence of numbers within a specified range. It is commonly utilized in for loops to iterate over a set of numbers, enabling repetitive execution of a block of code. Let's explore the different ways to use the range function and understand its applications through various examples.

Basic usage of range(stop)

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

In this example, the range(5) function generates the numbers 0, 1, 2, 3, and 4. The for loop iterates over these numbers and prints each one.

Output:
0
1
2
3
4

Specifying the start value with range(start, stop)

The range function can also accept a start value, which specifies the beginning of the sequence. The stop value remains the upper limit, which is not included in the sequence. This allows you to generate sequences that do not necessarily start from zero.

Example:
for i in range(2, 6):
print(i)

Here, the range(2, 6) function generates the numbers 2, 3, 4, and 5. The sequence starts from 2 and goes up to, but does not include, 6.

Output:
2
3
4
5

Controlling the increment with range(start, stop, step)

In addition to start and stop values, the range function can take a step value, which specifies the increment between numbers in the sequence. This is useful when you need to skip numbers or generate sequences with a specific interval.

Example with a Step Value:
for i in range(1, 10, 2):
print(i)

In this example, range(1, 10, 2) generates the numbers 1, 3, 5, 7, and 9. The sequence starts from 1 and increments by 2, stopping before it reaches 10.

Output:
1
3
5
7
9

Practical applications of the range function

The range function is often used in conjunction with for loops to iterate over a specific range of numbers. This is particularly useful in various programming tasks, such as iterating over list indices, generating sequences for mathematical operations, or simply repeating actions a specific number of times.

Example: Iterating Over List Indices
fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)):
print(f"Index {i} contains {fruits[i]}")

In this example, range(len(fruits)) generates a sequence of indices from 0 to 2 (since len(fruits) is 3). The for loop uses these indices to access and print each element of the fruits list.

Output:
Index 0 contains apple
Index 1 contains banana
Index 2 contains cherry
Example: Generating a List of Numbers
Sometimes, you might need to store the sequence of numbers generated by the range function. You can convert the range object to a list.
numbers = list(range(5))
print(numbers)

In this example, list(range(5)) converts the range object into a list containing the numbers 0, 1, 2, 3, and 4.

Output:
[0, 1, 2, 3, 4]

Quiz Question

What does the range(stop) form of the range function in Python do?

Quiz Question

Consider the following code snippet:

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

Quiz Question

What is the purpose of the step parameter in the range(start, stop, step) form of the range function?

In a nutshell…

The range function is a powerful and flexible tool in Python that facilitates iteration over sequences of numbers. By understanding its various forms—range(stop), range(start, stop), and range(start, stop, step)—you can efficiently control the flow of loops and handle a wide range of programming tasks. Whether you need to iterate over list indices, generate custom sequences, or perform repetitive actions, the range function is an indispensable part of your Python toolkit.