In this post, we will learn about7 ways to generate float range in Python or Python generate random float numbers in range with code examples. In Python, we will learn this by using list comprehension, numpy library arange(), linespace(), Round() function, and many other techniques.
Problem with Python range() function
The Python range() function takes three arguments, start, stop, and step integer arguments. If we pass a float number as an argument it throws a type error “TypeError: ‘float’ object cannot be interpreted as an integer”. Let us understand with an example.
start = 5
stop = 7.5
step = 0.3
for val in range(start, stop, step):
print (val)
Output
TypeError: 'float' object cannot be interpreted as an integer
1. list comprehension to generate float range in Python
In this example, we are using the List comprehension with range() function to generate a range of float numbers.
Python Program to generate float range
start = 3
stop = 30
step = 3
print("range of float number :")
[print(val /10.0, end=", ") for val in range(start, stop, step)]
Output
range of float number :
0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7,
2. map() with Lamda to generate float range in Python
In this code example, we are using the map() and lambda function to generate a range of float numbers.
Python Program to generate float range map()
print(list(map(lambda val: val/10.0, range(5, 50, 10))))
result_list = list(map(lambda val: val/10.0, range(5, 50, 10)))
for num in result_list:
print(num,end=",")
Output
[0.5, 1.5, 2.5, 3.5, 4.5]
0.5,1.5,2.5,3.5,4.5,
3. Round() function to generate float range in Python
Here, we are using Python’s built-in round() function to generate a range of float numbers.
Program Example
print("range of float number 1 to 10:")
[print(round(val* 0.10,2),end=", ") for val in range(1,10)]
Output
range of float number 0 to 10:
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
4. Numpy arange() function to generate float range in Python
As we can’t pass float type arguments to Python built-in function arange() whereas in Numpy arrange() function we can pass float number to start, stop, step arguments and it returns sequence range of floating number.
- To use Numpy we need to install it using the following command pip install numpy.
- Import the numpy module in our program using the code import numpy as np
Python Program float range of positive number
import numpy as np
#range of float number using Numpy.arange()
for num in np.arange(2.5, 27.5, 2.5):
print(num, end=', ')
Output
As we can see in the result it generated the float number from 2.5 to 25.0. It does not include the last value of 27.5 in the result because it never includes the stop value in the result.
2.5, 5.0, 7.5, 10.0, 12.5, 15.0, 17.5, 20.0, 22.5, 25.0,
Python Program to Generate Range of float negative number
In the above example, we are generating the positive range of float numbers. Sometimes instead of positive, we need to generate a negative range of float numbers.
import numpy as np
#range of negative float number using Numpy.arange()
for num in np.arange(-2.5, -25.5, -2.5):
print(num, end=', ')
Output
-2.5, -5.0, -7.5, -10.0, -12.5, -15.0, -17.5, -20.0, -22.5, -25.0,
Python Program to Pass float value in step parameter
We can pass the float step to np. arange() to generate a range of floating numbers.
import numpy as np
for num in np.arange(5, 20, 2.5):
print(num, end=', ')
Output
5.0, 7.5, 10.0, 12.5, 15.0, 17.5,
5. Numpy linespace() to generate float range in Python
By using the Numpy modules, linespace() function we can generate the range of float numbers.
Syntax
np.linspace(start,stop,count,enpoint)
Parameters :
- start : Starting value of sequence.
- stop: Optionlal End value of the sequence,default value is 50.
- num:The number of value to generate within given range.
- endpoint : If true end value of stop parmeter will be include in last value of sequnce else it will not.The default value of it is 0.
Python Example
import numpy as np
print(np.linspace(2.0, 10.0, num = 5))
print(np.linspace(4,24,num = 5, endpoint = False))
Output
[ 2. 4. 6. 8. 10.]
[ 4. 8. 12. 16. 20.]
6.itertool Module to generate a range of float number
The itertool Module isslice() method can be used to generate a floating-point range.We have to import this into the program to use it.
Program Example
import itertools
import math
def sequence(start, end, step):
if step == 0:
raise ValueError("step must be greater than 0")
stop = math.ceil(abs(end - start) / step)
return itertools.islice(itertools.count(start, step), stop)
for val in sequence(0, 5, 0.5):
print(val, end=", ")
Output
0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5,
7. yield generator to generate a range of float number
In this example, we are using the decimal module along with the yield generator to generate a range of float.
Example
import decimal
def generate_float_range(startval, stopval, step):
while startval < stopval:
yield float(stopval)
startval += decimal.Decimal(step)
print(list(generate_float_range(0, 5, 0.5)))
Output
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]