# Generate float range in Python [9 ways]

There are sometimes when you need to generate a range of floating-point numbers, for making various codes easier to implement and execute. Here, the focus is on generating a range of floating-point numbers.

This tutorial demonstrates the several methods available to generate a range of float-point numbers.

## What is a floating-point number in Python?

A floating-point number, typically categorized and stored under the `float` data type, is capable of representing real numbers that contain both integer and fractional parts. A `float` can store numbers containing decimal points without any errors or loss of data.

## Why not use the simple and popular `range()` function?

To simply carry out the operation of returning a sequence of numbers in Python, we typically use the `range()` function. However, the `range()` function is not applicable for use with the floating-point numbers and is incapable of accepting a floating-point number as its input.

If a floating-point number is entered as an input to the function, it automatically generates an error that displays the words `TypeError`.

## Python range float : How to generate floating-point numbers in Python?

1. Using `numpy.linspace()` method.
2. Using `numpy.arange()` method.
3. Using list comprehension.
4. Using generator comprehension.
5. Using yield generator.
6. Using a user-defined function
7. Using the `itertools` module and its functions.
8. Using the `map()` function along with a `lambda` function.
9. Using `round()` function.

### Using the `numpy.linspace()` method.

The `numpy.linspace()` method takes in the `start` and `stop` values and moves on to return a sequence of numbers as specified by the user.

Further specifications on this function are done by specifying the `num` argument that the function takes in, which is utilized to specify the number of values that need to be generated within the given range.

This method comes under the `NumPy` library, which is utilized in dealing with arrays and matrices and needs to be imported to the Python code before using this method.

The following code uses the `numpy.linspace()` method to generate a range of float-point numbers in Python.

The above code provides the following output:

array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

As seen in the above code, the `endpoint` argument can be optionally specified according to a particular user’s needs. This argument states whether the final value of the sequence needs to be included or not, and holds a default value of `0`.

### Using the `numpy.arange()` method.

The `numpy.arange()` method, similar to the `numpy.linspace()` method, takes in the `start` and `stop` values and moves on to return a sequence of numbers as specified by the user. However, instead of the `num` argument, the `numpy.arange()` method takes in another `step` argument that is utilized to specify the step size between the sequence.

By default, `start` holds the value `0` while `stop` holds the value `1`.

This method comes under the `NumPy` library and is not a default Python function, which means it is necessary to import the `NumPy` library to the Python code before using this method.

The following code uses the `numpy.arange()` method to generate a range of float-point numbers in Python.

The above code provides the following output:

[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]

### Using list comprehension.

List comprehension is a relatively shorter and very graceful way to create lists that are to be formed on the basis of given values of an already existing list.

This method makes a set of tweaks to enable implementing the given task with the help of the `range()` method.

With the help of list comprehension, we compactly divide all the results of the original `range()` function and convert it to the decimal form. Here, we divide the outcome of the `range()` function by 10 to keep things simple to understand, but any other number can be used instead.

The following code uses list comprehension to generate a range of float-point numbers in Python.

The above code provides the following output:

[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

### Using generator comprehension.

Generator comprehension works in a similar manner to list comprehension and uses a compact and syntactic approach in order to create an iterable generator object. The whole sequence can be looked at and generated with the help of the `for` loop along with Generator comprehension.

The following code uses generator comprehension to generate a range of float-point numbers in Python.

The above code provides the following output:

0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

### Using yield generator.

A simple iterable generator object can be further accessed and implemented with the help of generator comprehension. However, the yield generator is a significantly superior and reusable way for generating the range of floating-point values with the help of the three arguments `start`, `stop` and `step`.

The following code uses the yield keyword to create and implement a generator function that generates a range of float-point numbers in Python.

The above code provides the following output:

[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

### Using a user-defined function.

Instead of importing different modules and utilizing several functions from them, we can simply think of a logic and create a user-defined function that implements the given task.

The following code creates a user-defined function and uses it to generate a range of float-point numbers in Python.

The above code provides the following output

Printing
Test: 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1

### Using the `itertools` module and its functions.

The `itertools` module is a built-in model which is frequently used in the world of Python programming and it contains several function that enables us to work on iterators to generate complex iterators.

To implement the task of generating a sequence of floating-point numbers, we will use the `islice()` method from the arsenal of methods provided by the `itertools` module.

The following code uses the `islice()` method from the `itertools` module to generate a range of float-point numbers in Python.

The above code provides the following output:

0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

### Using the `map()` function along with a `lambda function`.

The `map()` function is provided by Python and it can be utilized to seek a particular specified function to all the existing elements in any given iterable. It produces the result as the iterator itself.

A lambda function can simply be defined as a compact anonymous function that is capable of taking any number of arguments while consisting of only a single expression.

The following code uses the `map()` function along with the `lambda function` to generate a range of float-point numbers in Python.

The above code provides the following output:

[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

### Using the `round()` function.

The `round()` function is an in-built function in Python that is utilized to round off the number given as the argument to a floating-point number.

The number of decimals can also be specified with this method, with the default value being `0`.

The `round()` function, therefore, takes in two arguments and its syntax looks like this.

Moving on, let us now see how we can use this function to generate a range of float-point numbers in Python.

The above code provides the following output:

[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

That’s all about Python range float.

import_contacts

## Related Posts

• 27 December

### Create Array of All NaN Values in Python

Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b407835540a691289067/] [crayon-63b4078355410558589250/] We used numpy.empty() […]

• 27 December

### Call Function from Another Function in Python

Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b40783555cd263514206/] [crayon-63b40783555d1523647143/] To call a function, […]

• 27 December

### [Fixed] NameError Name ‘unicode’ is Not Defined in Python

Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63b40783556f6141091719/] [crayon-63b40783556f9721510416/] Unicode is a computing industry standard that ensures that text from most of […]

• 26 December

### Repeat List N Times in Python

Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

• 26 December

### Get First Day of Next Month in Python

Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

• 25 December

### Convert String Array to Int Array in Python

Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.