How do I create a range of floating-point numbers?

To create a range of floating-point numbers in Python, you can use a loop or a list comprehension along with the `numpy` library. The `numpy` library provides great functionality for numerical operations, including generating ranges of floating-point numbers.

Here's a simple example using the `numpy` library:

import numpy as np # Create a range of floating-point numbers from 0 to 1 with a step of 0.1 float_range = np.arange(0, 1.1, 0.1) print(float_range)

You can also create a custom function without using external libraries:

def float_range(start, stop, step): current = start while current < stop: yield round(current, 2) current += step # Example usage for num in float_range(0, 1, 0.1): print(num)

Python floating-point numbers range numpy list comprehension