The range or sequence of numbers is a widely used concept in Programming languages. The “range()” function is used in Python to generate the sequence of numbers. But the simple “range()” function will return an error when the float value is passed to it, so we explained several other methods to create the range of float numbers. Some of the methods that are used to generate the float numbers range are “np.arange()”, “np.linspace()”, etc.

In this blog post, we will explain various methods on how to create a range of float numbers using numerous examples:

Method 1: Using np.arange() Function

To generate the range of float numbers, the “np.arange()” function is used in Python. The examples below illustrate how to create the range of floats in Python.

Example 1: Range For Float Number

In the code given below, the “np.arange()” function is utilized with the “for” loop to create the range of float number:

Code:

```import numpy

for items in numpy.arange(2.5, 10.5, 1.5):
print(items)
```

In the above code, the “np.arange()” function accepts the start, stop, and step size of the array in float data type and returns the array of float data within the specified range.

Output:

The above output shows the range of floats from “2.5” to “10.0”.

Example 2: Range For Float Number in Reversed Order

The range of floats can be created in reversed order using the negative step size value. Here is a code example:

Code:

```import numpy

for items in numpy.arange(10.5, 0.5, -1.5):
print(items)
```

In the above code, the “np.arange()” function takes the negative step size as an argument and returns the float range in reversed order.

Output:

The range of floats has been displayed on the screen in the reversed order.

Method 2: Using np.linspace() Function

The “np.linspace()” function is used to create an array according to the specified interval. The below code is used to create a range of float values:

Code:

```import numpy
value = numpy.linspace(0.5, 20.5, num=6)
for i in value:
print(i)
```

In the above code, the “np.linspace()” function accepts the start and stop interval as an argument which indicates the starting and ending interval position. The “num” parameter indicates the number of samples to create in the output.

Output:

The above output shows that the “6” samples are generated for a float range between “0.5” to “20.5”.

Method 3: Using List Comprehension Method

The “List Comprehension” method is used along with a for loop to get the range of float numbers in Python. Let’s comprehend this method via the given below example code:

Code:

```output = [x / 10 for x in range(5, 100, 15)]
print(output)
```

In the above code, the “x” value is divided by 10 to generate the float range for the given numbers.

Output:

The above output shows the floating point range from “0.5” to “9.5”.

Method 4: Using Yield

The “yield” keyword is very similar to the return statement in Python. It is normally used with a generator function to generate the value. The “yield” keyword is used along with a function of the decimal module to create the range of float values in Python:

Code:

```import decimal

def range_float(start_value, stop_value, step_value):
while start_value < stop_value:
yield float(start_value)
start_value += decimal.Decimal(step_value)

print(list(range_float(2, 10, '1.5')))
```

In the above code, the user-defined function “range_float()” is defined in the program. The “yield” keyword inside the function generates the starting number of the sequence, and the “decimal.Decimal()” function is used to create the floating sequence according to the step size interval.

Output:

The above output displays the range of float numbers.

Conclusion

To create the range of float numbers, the “np.arange()” function, “np.linspace()”  function, list comprehension method, and Yield keyword are used in Python. The “np.arange()” function creates the floating point range by accepting the float interval size as an argument. Similarly, the “np.linspace()” function returns the range of floats according to a specified number of samples. This post presented a detailed guide on creating a range of float numbers in Python using numerous examples.

var authorName = "' . esc_js(\$post_author) . '";'; ?>