How To Use The Range() Function In Python?

The range() function in Python is a versatile tool for creating sequences of numbers within a specified range.

It's commonly used in for loops and other scenarios where a sequence of numbers is required.

In this guide, we'll explore the syntax, usage, and examples of the range() function to help you master this essential Python function.

Syntax of the range() Function:

The syntax of the range() function is as follows:

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

Basic Examples of the range() Function:

Example 1: Using Only the Stop Parameter

# Using range() with only the stop parameter
result = list(range(5))

print(result)

The output will be:

[0, 1, 2, 3, 4]

In this example, range(5) generates a sequence of numbers from 0 to 4 (exclusive).

Example 2: Specifying Start and Stop Parameters

# Using range() with start and stop parameters
result = list(range(2, 8))

print(result)

The output will be:

[2, 3, 4, 5, 6, 7]

Here, range(2, 8) generates a sequence of numbers from 2 to 7.

Example 3: Adding the Step Parameter

# Using range() with start, stop, and step parameters
result = list(range(1, 10, 2))

print(result)

The output will be:

[1, 3, 5, 7, 9]

In this example, range(1, 10, 2) generates a sequence of odd numbers from 1 to 9.

Using range() in For Loops:

The primary use of the range() function is in for loops to iterate over a sequence of numbers. Here's a simple example:

# Using range() in a for loop
for i in range(3):
    print(f"Iteration {i}")

The output will be:

Iteration 0
Iteration 1
Iteration 2

In this example, the for loop iterates over the sequence range(3).

Practical Examples of range() in Loops:

Example 1: Summing Numbers in a Range

# Summing numbers in a range
total = 0

for number in range(1, 6):
    total += number

print("Sum:", total)

The output will be:

Sum: 15

In this example, the for loop iterates over the numbers from 1 to 5, and the sum is calculated.

Example 2: Printing Multiples of a Number

# Printing multiples of a number in a range
multiple = 3

for i in range(1, 6):
    print(f"{multiple} x {i} = {multiple * i}")

The output will be:

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15

In this example, the for loop prints multiples of the number 3 in the specified range.

Using range() with len() Function:

The range() function is often used in conjunction with the len() function to iterate over indices of a sequence. Here's an example:

# Using range() with len() to iterate over a list
fruits = ['apple', 'banana', 'orange']

for i in range(len(fruits)):
    print(f"Index {i}: {fruits[i]}")

The output will be:

Index 0: apple
Index 1: banana
Index 2: orange

In this example, the for loop iterates over the indices of the fruits list using range(len(fruits)).

Conclusion:

The range() function is a powerful tool in Python for generating sequences of numbers within a specified range.

Whether you're using it in for loops to iterate over a sequence or in other scenarios where a sequence of numbers is needed, understanding the syntax and capabilities of the range() function is essential for writing efficient and readable code.

As you explore more complex programming tasks in Python, the range() function will continue to be a valuable asset in your coding toolkit. Happy coding!