How to Generate a Range of Floats in Python

To create a range of floats we will first need to define a start, end and step value for each float in the range.

 

We can use the NumPy arange() function to create the range of floats and finally convert the object into a Python list.

 

Import NumPy and Generate a Range

The first step is to import numpy as np. Then pass the start as the first argument, the end as the second and the step as the third argument to the np.arrange() function and store the result in a variable.

 

To convert the NumPy object into a regular list, use the Python list() function. Pass the object as the first argument and store the result in a variable.

 

import numpy as np

start = 1.0
stop = 5
step = 0.5

float_obj = np.arange(start, stop, step)
float_list = list(float_obj)

print(float_list)
[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]