In this post, we are going to understand How to use range in For loop in Python that includes a negative range in Python, reverse range in Python, range to list in Python, range inclusive, float range in Python, if-else range with code examples.
Python Range() function
Unlike other programming languages, Python does not have an in-built numeric loop. So this can be achieved by using Python’s built-in range() function.
Numeric for Loop in C#, Java, C++
for(int i=0;i<5;i++)
The range() function comes in handy, whenever we have to run a for loop for a numeric sequence. It generates a sequence of integer numbers between starting given index 0 by default, increment by 1 to stop integer number that is stop-1 by default.The Python3 range() function is a rename version of Python 2 xrange().We can decide the start, stop end numbers.
Syntax
range(start,stop,step)
Parameters
- Start: starting integer number from which sequence of integer numbers to be returned, by default it is 0.
- End: optional integer number before which sequence of integer to be returned. The range of integers will end at end stop -1.
- Step: It is an optional parameter that represents an integer number that controls the increment between each number in the sequence by default value is 1.it raises ValueError if passed 0.
Returns values
It returns a range of integer sequence numbers as per given arguments.
1. For loop Using range(stop)
In this example, we have only passed the stop argument to the range() function. It generates sequence numbers starting from 0, increment by 1, and stop at stop -1. Because the default value of the start argument is 0 and step is 1 so increment by 1 and stop-1.
Example -For loop Using range(stop)
for val in range(5) :
print(val,end=",")
Output
0,1,2,3,4,
2. For loop range(start,stop)
In this example, we have passed the start, stop argument to the range() function. It generates sequence numbers starting from 2, increment by 1, and stop at stop-1.
for val in range(2,10) :
print(val,end=",")
Output
2,3,4,5,6,7,8,9,
3. For loop with range(start,stop,step)
In this example, we have passed start, stop step arguments to the range() function. It generates sequence integer numbers starts from 3 , increment by 3, and stop at stop-3 that would be 24-3
for val in range(3,24,3) :
print(val,end=",")
Output
3,6,9,12,15,18,21,
4. Python if range()
In this example, we will understand with example how to use if statement with range() function
Program Python if range()
range_val = range(3, 20, 3)
input_num = int(input('Please Enter a number : '))
if input_num in range_val :
print(input_num, 'is existt in the range.')
else :
print(number, 'is not exist in the range.')
Output
Please Enter a number : 15
15 is existt in the range.
Python if-else statement with range()
In this example, we will understand how to use Python if-else statement with range().
num = int(input("Enter the number:"))
if num in range(1, 10):
print(" number in the range of 1 to 10")
elif num in range(10, 21):
print("number in the range of 10 to 20")
else:
print("Number does not fall in any range")
Output
Enter the number:12
number in the range of 10 to 20
5. Reverse range in Python
We can generate a range of sequence numbers in descending order by using the range() function in these two ways.
- Negative step value
- Reversed() function
1- Negative step value :
We can use the negative step in range() function to generate series of numbers in descending or reverse order. Let us understand with for loop with range() function. In this example, we are setting a negative step argument that makes for loop iterate in reverse order
Program Negative step value To reverse
for val in range(10, 1,-2):
print(val,end=",")
Output
10,8,6,4,2,0,
2- Reversed() Method to Reverse range in Python:
The reversed() function allows us to return a reversed iterator of the passed sequence(tuple, string, list, range).
In this example, we are passing range() function as an sequence agrument to reversed() function.It returns <class,’range_iterator’> of range of elements in reverse order.Using the for loop to iterate over result returns by reversed() method.
for val in reversed(range(0,10,2)):
print(val,end=",")
Output
8,6,4,2,0,
6. Negative Range() in Python
We can generate a negative sequence of numbers using the range() function. We passed negative start, step, stop arguments.
Program Example negative range
for val in range(-1,-13,-2):
print(val,end=", ")
Output
-1, -3, -5, -7, -9, -11,
Negative reverse range in Python
In this program, we are generating a negative reverse range starting from -13 to 0.
for val in range(-13,-0):
print(val,end=", ")
Output
-13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1,
Positive and negative number range
In this example, we are generating Positive and negative number range.
for val in range(3, -9, -2):
print(val,end=", ")
Output
3, 1, -1, -3, -5, -7,
7.Python range() step argument
It is an optional parameter that represents an integer number that controls the increment between each number in the sequence by default value is 1.It raises ValueError if passed 0.
Program Example using default step value
for val in range(9):
print(val,end=", ")
print("pass custom step value:")
for val in range(5,50,5):
print(val,end=", ")
Output
0, 1, 2, 3, 4, 5, 6, 7, 8,
pass custom step value:
5, 10, 15, 20, 25, 30, 35, 40, 45
8. Python range() to List
Sometimes we need to generate a list that contains a continuous value. We can use the range() function to achieve this.
Using List constructor
We passed the range() function to list constructors that return a list constructed from the range() function.
#python 3 program to convert range to list
range_to_list = list(range(2, 16, 2))
print(range_to_list)
Output
[2, 4, 6, 8, 10, 12, 14]
Using the unpacking operator (*)
We can also convert range() to list using the unpacking operator.To create a list from range we pass range() function to list literal([])
#Program to convert range to list python 3
range_to_list = [*range(5)]
print(range_to_list)
Output
[0, 1, 2, 3, 4]
9.How to generate inclusive range() in Python
The default nature of range() is exclusive it does not include the last value in the output sequence. Because it generates a number of sequences from start 0 index by default to stop-1.
If we need to generate a number range (2,10,2) it will generate a sequence of numbers start from 2 and end at 8 that is 10-2. (2, 4, 6, 8,) So it excluded the last value.
To make it inclusive we have to add step value to stop that needs to be passed to range() function.
#python 3 program to convert range insclusive range
start = 2
stop = 10
step = 2
stop = stop + step
for val in range(start, stop, step):
print(val, end = ", ")
Output
2, 4, 6, 8, 10,
10.Range Of Float Numbers In Python
The Python range() function throws an error if we pass float number in the start, stop, stop. So we have to do some workaround to generate a range of float numbers.