You are currently viewing Python range() with float values

Let’s see how to return the range of float values in Python by using the numpy.arange() and numpy.linspace() methods.

Advertisements

Python range() will not be applicable to float values hence, you have to use the NumPy methods to get a list of float values. Using range() with float returns a TypeError.

1. Quick Examples of Python range() with Float values

Following are quick examples of how to generate float values using the numpy.arange() and linspace() methods.


# Quick examples of getting range of float values
import numpy as np

# np.arange() without step parameter
for i in np.arange(0.5,5.5):
    print(i, end=', ')

# np.arange() with step parameter
for i in np.arange(0.5,5.5,1.5):
    print(i, end=', ')

# Using np.linspace()
for i in np.linspace(1.5, 5.5):
    print(i, end=', ') 

# Using np.linspace() with num parameter
for i in np.linspace(1.5, 5.5,num=5):
    print(i, end=', ')  

2. Python range() with Float (Raises TypeError)

The range() function in python is used to generate values within the specified range of integers but not float. It will take only integer values. If we pass the float value, TypeError will be returned.

Example:

Let’s try to generate values from 0 to 4.5 with step as 0.5


# range()
for i in range(0, 4.5, 0.5):
    print(i, end=', ')

You can see TypeError is returned.

3. Using numpy.arange()

As range() doesn’t support float values, you can use the NumPy arange() method which will generate the range of float numbers. Unlike the range() function, It will support generating float and integer values.

We will specify this method in the for loop to generate the range of float values. It will take the same parameters as range() function.

3.1 numpy.arange() Syntax

The syntax for numpy.arange() method.


# Syntax of numpy.arange()
numpy.arange(start,stop,step):

3.2 numpy.arange() Parameters

  1. start parameter specifies the starting value
  2. stop parameter specifies the end value of the range
  3. step will increment the value within the specified range.

3.3 NumPy arange() – Generates Range of Float Values

Example 1: Let’s use the Python NumPy arange() function to generate a range of float values by specifying only start and stop parameters. This by default uses 1 as the increment/step value.


import numpy as np

# np.arange() without step parameter
for i in np.arange(0.5,5.5):
    print(i, end=', ')

# Output:
# 0.5, 1.5, 2.5, 3.5, 4.5, 

We specified the start as 0.5 and the stop as 5.5. So It generated values from 0.5 to 5.5 by incrementing 1.

Example 2: Let’s generate float values by specifying all three parameters.


import numpy as np

# np.arange() with step parameter
for i in np.arange(0.5,5.5,1.5):
    print(i, end=', ')

# Output:
# 0.5, 1.5, 2.5, 3.5, 4.5, 

We specified the start as 0.5, the stop as 5.5, and the step as 1.5. So It generated values from 0.5 to 5.5 by the increment of 1.5

4. Using numpy.linspace()

The linspace() function in numpy is also used to create the sequences of evenly spaced values within a defined interval.

These intervals are passed by using the start and stop parameters. It will also take two optional parameters like num and endpoint.

4.1 numpy.linspace() Syntax

Syntax for numpy.linspace() method.


# Syntax
numpy.linspace(start,stop,num,endpoint):

4.2 numpy.linspace() Parameters

  1. start parameter specifies the starting value
  2. stop parameter specifies the end value of the range
  3. num will generate a total number of specified within the range of values. By default, it is 50.
  4. endpoint is used to stop the last sample. By default it is True.

4.3 numpy.linspace() Examples

Example 1: Let’s generate float values by specifying only start and stop parameters.


import numpy as np

# Using np.linspace()
for i in np.linspace(1.5, 5.5):
    print(i, end=', ')

# Output:
# 1.5, 1.5816326530612246, 1.663265306122449, 1.7448979591836735, 1.8265306122448979, 1.9081632653061225, 1.989795918367347, 2.071428571428571, 2.1530612244897958, 2.2346938775510203, 2.316326530612245, 2.3979591836734695, 2.479591836734694, 2.561224489795918, 2.642857142857143, 2.724489795918367, 2.8061224489795915, 2.887755102040816, 2.9693877551020407, 3.0510204081632653, 3.13265306122449, 3.2142857142857144, 3.2959183673469385, 3.377551020408163, 3.4591836734693877, 3.5408163265306123, 3.6224489795918364, 3.704081632653061, 3.7857142857142856, 3.86734693877551, 3.9489795918367343, 4.030612244897959, 4.112244897959183, 4.1938775510204085, 4.275510204081632, 4.357142857142857, 4.438775510204081, 4.520408163265306, 4.6020408163265305, 4.683673469387754, 4.76530612244898, 4.846938775510203, 4.928571428571429, 5.0102040816326525, 5.091836734693877, 5.173469387755102, 5.255102040816326, 5.336734693877551, 5.418367346938775, 5.5, 
 

We specified the start parameter as 1.5 and the stop parameter as 5.5. It generated the values within these ranges.

Example 2: Let’s generate float values by specifying the num parameter.


import numpy as np

# Using np.linspace() with num parameter
for i in np.linspace(1.5, 5.5,num=5):
    print(i, end=', ')

# Output:
# 1.5, 2.5, 3.5, 4.5, 5.5,

We specified the num parameter and set it to 5. So only 5 values are generated within the given range.

5. Conclusion

You have learned that the Python range() function won’t support generating the float values. To overcome this issue we used the NumPy module methods, numpy.linspace() and numpy.arange(). For both these methods, the first two parameters are common that will specify the start range and stop range.