range() Function

The range() function in Python is used to generate a sequence of numbers, which is commonly used in loops like for loops. It allows you to specify a start value, an end value, and a step value to control the sequence of numbers generated. The range() function is often used in scenarios where you need to iterate over a sequence of numbers or indices.

Syntax

The range() function has the following syntax:

range(start, stop, step)
  • start (optional): The starting value of the sequence (default is 0).
  • stop: The ending value of the sequence (the sequence will include numbers up to, but not including, this value).
  • step (optional): The increment between each number in the sequence (default is 1).

Examples

1. Basic Usage

Generate a sequence of numbers from 0 to 4.

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

Explanation:

  • range(5) generates numbers from 0 to 4. The for loop iterates over each number and prints it.

2. Specifying a Start Value

Generate a sequence of numbers starting from a value other than 0.

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

Explanation:

  • range(2, 6) generates numbers from 2 to 5. The loop prints these numbers.

3. Specifying a Step Value

Generate a sequence of numbers with a specified step value.

for i in range(0, 10, 2):
    print(i)

Explanation:

  • range(0, 10, 2) generates numbers from 0 to 8, with a step of 2. The loop prints 0, 2, 4, 6, and 8.

4. Negative Step

Generate a sequence of numbers in reverse order using a negative step.

for i in range(10, 0, -2):
    print(i)

Explanation:

  • range(10, 0, -2) generates numbers from 10 to 2, with a step of -2. The loop prints 10, 8, 6, and 4.

5. Using range() with Lists

You can use range() to iterate over the indices of a list.

fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)):
    print(f"{i}: {fruits[i]}")

Explanation:

  • range(len(fruits)) generates indices for the list fruits. The loop prints the index along with the corresponding element.

Important Points

  • The stop value is exclusive, meaning the sequence will not include the stop value itself.
  • The start and step values are optional. If not provided, the default start value is 0, and the default step value is 1.
  • range() returns a range object, which is an immutable sequence. If you need a list of numbers, you can convert it using list(range(...)).

Example:

numbers = list(range(5))
print(numbers)  # Output: [0, 1, 2, 3, 4]

range() with Negative Step

The range() function can also be used with a negative step to generate sequences in reverse order.

for i in range(10, 0, -1):
    print(i)

Explanation:

  • This generates a sequence from 10 down to 1 with a step of -1.

Note:

  • If the start value is greater than the stop value and the step is positive, or if the start value is less than the stop value and the step is negative, an empty range is returned.
Task

Practice: Using range() Function

Objective: Practice using the range() function in Python.

  1. Create a Python File: Create a file named range_practice.py.

  2. Basic Loop: Use range() to print numbers from 0 to 9.

    Example:

    for i in range(10):
        print(i)
    
  3. Custom Range: Use range() to print numbers from 5 to 15 (inclusive) with a step of 3.

    Example:

    for i in range(5, 16, 3):
        print(i)
    
  4. Reverse Range: Use range() with a negative step to print numbers from 20 to 10.

    Example:

    for i in range(20, 9, -2):
        print(i)
    
  5. List Indices: Use range() to iterate through the indices of a list and print each element.

    Example:

    colors = ['red', 'green', 'blue']
    for i in range(len(colors)):
        print(f"{i}: {colors[i]}")
    
  6. Convert to List: Convert the range to a list and print it.

    Example:

    print(list(range(5)))
    

Copyright © 2025 Devship. All rights reserved.

Made by imParth