You are currently viewing range() in for loop in Python

How to use Python range() in for loop? The range() function is typically used with for loop to repeat a block of code for all values within a range. Let’s discuss different scenarios of using the range() function with for loop in Python. By specifying start, stop, and step parameters, By excluding the start and step parameters and excluding step parameters. We will also discuss how to pass the negative step value in the range() function.

Advertisements

1. Quick Examples of using range() in the for loop

Following are quick examples of range() with the for loop.


# Example 1: range() with stop parameter
for i in range(10):
    print(i)

# Example 2: range() with start and stop parameters
for i in range(1,10):
    print(i)

# Example 3: range() with start,stop and step parameters
for i in range(1,10,2):
    print(i)

# Example 4: range() with negative step
for i in range(20,10,-3):
    print(i)

2. Using range() in for loop

You can use the range function in a for loop in Python to repeat a block of code a specific number of times. The range function generates a sequence of numbers, starting from 0 by default, and increments by 1 (also by default), and stops before a specified number.

2.1 Syntax

Let’s see the syntax of range() with the for loop.


# Syntax
for element in range(start,stop,step):
    statements...

2.2 range() with for Loop Examples

Let’s specify start and stop parameters in the range() function and iterate them using the for loop. This is typically used when you wanted to run a block of code a certain number of times.


# range() with start and stop parameters
for i in range(1,10):
    print(i,end=",")

# Output:
# 1,2,3,4,5,6,7,8,9,

Here, I have specified the start as 1 and the stop as 10 on range(). The for loop will iterate from 0 to 9 with the default step value as 1.

3. Iterate for Loop range with Only Stop Value

Let’s specify only stop parameter in the range() function and iterate them using the for loop. Here, by default, it uses the start param as 0 and the step value as 1.


# range() with stop parameter
for i in range(10):
    print(i,end=",")

# Output:
# 0,1,2,3,4,5,6,7,8,9,

We specified the stop as 10. The for loop will iterate from 0 to 9. Inside the for loop, we are displaying the values present in the range.

4. Iterate range() with All Parameters

Now, let’s specify all parameters start, stop, and step in the range() function with the Python for loop and iterate the range values using the for loop. Note that here, I have used the step value as 2, so it increments by 2.


# range() with start,stop and step parameters
for i in range(1,10,2):
    print(i,end=",")

# Output:
# 1,3,5,7,9,

5. Iterate range() in Reverse

To decrement the range of values in for loop, use the negative value as a step parameter. Let’s specify the negative step value in the range() function and iterate them with for loop.


# range() with negative step
for i in range(20,10,-3):
    print(i,end=",")

# Output:
# 20,17,14,11,

We have iterated from 20 to 10 (Reverse order) by decrementing 3 at each step. The final iterated values are – 20,17,14 and 11.

Alternatively, to get a reverse range in Python, you can use the reversed() function in combination with the range function. For example:


# Using reversed() & range()
for i in reversed(range(10)):
    print(i)

This would print the numbers from 9 to 0 (inclusive).

6. Conclusion

We have seen how to use the Python range() in the for loop by considering four different scenarios. It is important to pass the stop parameter in the range() function. We also discussed different parameters of range() and how to pass negative value in the step parameter.