This article explains **how to generate a range for Float Numbers in python**. I have written a couple of example programs in python which will demonstrate how to **use a floating-point number in a start, stop and step argument** of custom range() function to print a range of floating-point numbers.

- The article is part of
**Python Basics**. **Solve our Python Basic Exercise for Beginners and**

**Python Quiz For Beginners to practicing Python essential skills.**

Examples mentioned in this article apply to all versions of python i.e., Python 3 and 2. Before moving to example first understand the limitation of the existing range() function to handle float values.

## Limitation of python’s range() function

**The main limitation of Python’s range() is it works only with integers**. Python range() doesn’t support the float type i.e., we cannot use floating-point or non-integer number in any of its argument.

For example, Let’s see the following source code.

for num in range(0, 5.5, 0.1): print (num)

Now, Let see how to generate the range for float numbers? **There are various ways to do this Let see one by one with the examples**.

## Use numpy’s arange() function to generate the range for float numbers in Python.

We can use the numpy module of Python programming language to get the range of floating-point numbers.

- NumPy library has various numeric functions and mathematical functions to operate on multi-dimensional arrays and matrices.

NumPy has the ** arange()** function to get the range of a floating-point number.

`arange()`

**function has the same syntax and functionality as python range() function**. Additionally, it also supports floating-point numbers in any of its argument. i.e., we can pass float number in the start, stop and step arguments.

**Syntax of numpy’s arange() function**: –

arange (start, stop, step)

Let demonstrate the use with the following example.

import numpy print("Printing float range with numpy.arange()") print("Example one") for i in numpy.arange(0, 5.5, 0.5): print(i, end=', ') print("\nExample two") for i in numpy.arange(5.5, 15.5, 2.5): print(i, end=', ') print("\nExample Three") for i in numpy.arange(-2.5, 2.5, 0.5): print(i, end=', ')

**Output**: **Execute Online**

Printing float range with numpy.arange() Example one 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, Example two 5.5, 8.0, 10.5, 13.0, Example Three -2.5, -2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0,

**Note**: NumPy isn’t a part of a standard library of python (i.e., it doesn’t come with default python installation ) but is relatively easy to use.

- To use this function you need to install NumPy module using pip.
`pip install numpy`

. - A numpy is a third party package and adds overhead concerning dependency-management when you want to use an
`arange()`

function.

So if you don’t want to use NumPy just for `arange()`

function, you can code your function to generate a range of float numbers. I have created such examples.

## The range for float numbers in Python without using any library or module

I have written a sample code which uses simple logic to generate the range of floating-point numbers. This code works for both positive and negative float values.

This Program divided into 2 Sections. the first section contains **function frange()**. another section contains testing a `frange()`

using Floating point number with the following approaches.

- Positive float values in range() arguments.
- With negative float values in range() arguments.
- Negative and positive float step value in
`frange()`

arguments. - Display float range in reverse order.

**Let see the example now.**

def frange(start, stop=None, step=None): #Use float number in range() function # if stop and step argument is null set start=0.0 and step = 1.0 if stop == None: stop = start + 0.0 start = 0.0 if step == None: step = 1.0 while True: if step > 0 and start >= stop: break elif step < 0 and start <= stop: break yield ("%g" % start) # return float number start = start + step #end of function frange() print ("range for floating point numbers") for i in frange(1.5, 5.5, 0.5): print (i, end=", ") print ("\n Using negative floating point numbers in range") for i in frange(-0.1, -0.5, -0.1): print (i, end=", ") print ("\n Printing Reverse float range") for num in frange(0.5, 0.1, -0.1): print (num, end=", ") # Other test scenarios print ("\n Printing float range") for num in frange(7.5): print (num, end=", ") print ("\n Printing float range") for num in frange(2.5, 7.5): print (num, end=", ")

**Output**: **Execute Online**

range for floating point numbers 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, Using negative floating point numbers in range -0.1, -0.2, -0.3, -0.4, Printing Reverse float range 0.5, 0.4, 0.3, 0.2, 0.1, Printing float range 0, 1, 2, 3, 4, 5, 6, 7, Printing float range 2.5, 3.5, 4.5, 5.5, 6.5,

### The range for float numbers using Python Generator

You can define a generator same as python range(), i.e., `range(start, stop, step)`

that accepts floating or fraction numbers and produces numbers. In this example, I am using types `+`

and `<`

for positive values and type `-`

and `>`

for negative float values.

I have created two separate function frange() for both the positive and negative float step.

**Program to generate a range of only positive float numbers using a generator.**

def range_positve(start, stop=None, step=None): if stop == None: stop = start + 0.0 start = 0.0 if step == None: step = 1.0 while start < stop: yield start start += step print ("Using Positive float number in range function") for x in range_positve(0.1, 1.0, 0.1): print ("%g" % x, end=", " )

Output: **Execute Online**

Using Positive float number in range function 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

**Program to generate a range of only negative float numbers using a generator.**

def frange_negative(start, stop, step): if stop == None: stop = start + 0.0 start = 0.0 if step == None: step = 1.0 while start > stop: yield start start += step print ("\nPrinting float range of negative float numbers") for x in frange_negative(-0.1, -0.5, -0.1): print ("%g" % x,end=", " )

Output: **Execute Online**

Printing float range of negative float numbers -0.1, -0.2, -0.3, -0.4,

### Use float number in step argument of range() function

Same like integer step value we can use floating-point step in your custom range function. Using float step size, you can generate floating point numbers of a specific interval. Let see how to use floating point step in python range() with an example program. In this example, the step value is 2.5.

import numpy as pynum_float print("Printing range using float step\n", pynum_float.arange(7, 25, 2.5) "\n")

You should get the following output.

Printing floating-point range with float step [ 7, 10, 12.5, 15, 17.5, 20, 22.5]

### Generate floating-point range using a itertools in Python

import itertools def seq(start, end, step): assert(step != 0) sample_count = abs(end - start) / step return itertools.islice(itertools.count(start, step), sample_count) for i in seq(0, 1, 0.1): print (i)

Output:

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

### Another example

The following code is useful for those who are not much familiar with python.

def seq_floats(start, stop, step=1): stop = stop - step; number = int(round((stop - start)/float(step))) if number > 1: return([start + step*i for i in range(number+1)]) elif number == 1: return([start]) else: return([]) print ("printing floating poinnt range for seq(1, 5, 0.5) -> ", seq_floats(1, 5, 0.5)) print ("printing floating poinnt range for seq(10, 0, -1) -> ", seq_floats(10, 0, -1)) print ("printing floating poinnt range for seq(10, 0, -2) -> ", seq_floats(10, 0, -2)) print ("printing floating poinnt range for seq(1,1) -> ", seq_floats(1,1))

Let execute above code to get the range of positive and negative decimal point numbers.

Output:

printing floating poinnt range for seq(1, 5, 0.5) -> [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5] printing floating poinnt range for seq(10, 0, -1) -> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] printing floating poinnt range for seq(10, 0, -2) -> [10, 8, 6, 4, 2] printing floating poinnt range for seq(1,1) -> []

## Next Steps

To practice what you learned in this article, I have created a Python Basic **Quiz and Exercise**.

- Solve our
**Python essential exercise for beginners**to have a better understanding of Python basic concepts. - Solve our
**Basic Python Quiz for beginners**to test your fundamental Python skills.

Let me know your comments and feedback in the section below.

April

Thank you, This is was i want. now i can use generate a range for float numbers. i am using numpy’s arange() for this.

Vishal

Thank You