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

How to use Python range() in a while loop? The range() function is typically used with a while loop to repeat a block of code for all values within a range. Let’s discuss different scenarios of using the range() function with a while 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 while loop

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


# range() with stop parameter
i=0
while i in range(10):
   print(i,end=" ")
   i=i+1

# range() with start and stop parameters
i=0
while i in range(5,10):
   print(i,end=" ")
   i=i+1

# range() with all parameters
i=0
while i in range(0,10,3):
   print(i,end=" ")
   i=i+3

2. Using range() in while loop

You can use the range function in a while 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.

It is very important to initialize the start variable and should be equal to the start parameter specified inside the range() function. Also, the step value is equal to the incrementing value inside the while loop. By default, the increment value is set to 1 for the step parameter.

2.1 Syntax

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


# While loop with range()
iterator=start_value
while iterator in range(start,stop,step):
   statements...
   increment/decrement

2.2 Examples

Example 1: Let’s specify only the stop parameter in the range() function.


# range() with stop parameter
i=0
while i in range(10):
   print(i,end=" ")
   i=i+1

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

Here, we specified stop as 10. So it will return the values starting from 0 to 9.

Example 2: Let’s specify start and stop parameters in the range() function.


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

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

Here, we specified start as 0 and stop as 10. So it will return the values starting from 0 to 9.

Example 3: Let’s specify start and stop parameters in the range() function.


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

# Output:
# 5 6 7 8 9 

Here, we specified start as 5 and stop as 10. So it will return the values starting from 5 to 9.

You can observe that initially ‘i’ value is initialized to 5 i.e equal to the start parameter value.

Example 4: Let’s specify all the parameters in the range() function.


# range() with all parameters
i=0
while i in range(0,10,3):
   print(i,end=" ")
   i=i+3

# Output:
# 0 3 6 9 

Here, we specified start as 0, stop as 10 and step as 3. So it will return the values starting from 0 to 9 with an increment of 3.

You can observe that initially ‘i’ value is initialized to 0 i.e equal to the start parameter value and stop parameter value is equal to increment value inside the while loop.

3. Conclusion

We have seen how to use the Python range() in the while loop by considering three 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 within while loop. make sure that start parameter value must be equal to the initial value and stop parameter value is equal to the incrementing/decrementing value.