Rather than using a decimal step directly, it's much safer to express this in terms of how many points you want. Otherwise, floating-point rounding error is likely to give you a wrong result.

Use the linspace function from the NumPy library (which isn't part of the standard library but is relatively easy to obtain). linspace takes a number of points to return, and also lets you specify whether or not to include the right endpoint:

>>> np.linspace(0,1,11)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ])
>>> np.linspace(0,1,10,endpoint=False)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])

If you really want to use a floating-point step value, use numpy.arange:

>>> import numpy as np
>>> np.arange(0.0, 1.0, 0.1)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])

Floating-point rounding error will cause problems, though. Here's a simple case where rounding error causes arange to produce a length-4 array when it should only produce 3 numbers:

>>> numpy.arange(1, 1.3, 0.1)
array([1. , 1.1, 1.2, 1.3])
Answer from Andrew Jaffe on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-range-for-float-numbers
Python - range() for Float Numbers - GeeksforGeeks
July 23, 2025 - We created a function float_range() that yields values from start to stop with a step size of 0.5.
Discussions

How to step by floats in a range function?
A somewhat “hacky” way to accomplish this using range directly is by multiplying start/end/step by a power of 10 so they’re all integers, then dividing by that same power in your loop. Do note that range doesn’t include the right endpoint. e.g. for degrees in range(0,3600,225): print(degrees/10) You could also form the step-size on your own and use range to get a multiple of it. E.g. if I wanted to cut [1,2] into 17 pieces, I could do for multiple in range(18): x = 1+(multiple/17) print(x) or just use numpy.linspace. More on reddit.com
🌐 r/learnpython
10
5
November 11, 2020
python - Range with step of type float - Stack Overflow
The documentation basically says that range must behave exactly as this implementation (for positive step): def range(start, stop, step): x = start while True: if x >= stop: return ... More on stackoverflow.com
🌐 stackoverflow.com
Float contained in range - Ideas - Discussions on Python.org
I have found that checking if a float number falls inside a range() doesn’t work, always giving False: >>> 1.0 in range (0, 2) True >>> 1.5 in range (0, 2) False I can understand that range() can only accept integer values, but checking if it contains a float number should works. More on discuss.python.org
🌐 discuss.python.org
0
September 7, 2023
float numbers with range () function
Yes, range(0, 101) literally gives the integers from 0 to 100. If you want to know if a number is between two values, use a chained comparison: while 0 <= score < 101: Although note you shouldn't get an actual error, just a False. What exact error message did you see? More on reddit.com
🌐 r/learnpython
4
2
April 19, 2022
🌐
TechBeamers
techbeamers.com › python-float-range
Generate Floating Point Range in Python - TechBeamers
November 30, 2025 - The first defines the function float_range(). Another one calls it with different input values and prints the result. """ Desc : This function generates a float range of numbers w/o using any library. Params : A (int/float) : First number in the range L (int/float) : Last number in the range D (int/float) : Step or the common difference """ def float_range(A, L=None, D=None): #Use float number in range() function # if L and D argument is null set A=0.0 and D = 1.0 if L == None: L = A + 0.0 A = 0.0 if D == None: D = 1.0 while True: if D > 0 and A >= L: break elif D < 0 and A <= L: break yield ("%g" % A) # return float number A = A + D #end of function float_range() """ Desc: This section calls the above function with different test data.
🌐
PYnative
pynative.com › home › python › python range of float numbers
Python range of float numbers
April 13, 2021 - It doesn’t support the float type, i.e., we cannot use floating-point/decimal value in any of its arguments. For example, If you use range() with float step argument, you will get a TypeError 'float' object cannot be interpreted as an integer.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python range() with float values
Python range() with float values - Spark By {Examples}
May 31, 2024 - # Syntax of numpy.arange() ... range. Example 1: Let’s use the Python NumPy arange() function to generate a range of float values by specifying only start and stop parameters....
Find elsewhere
🌐
Codingem
codingem.com › home › range of floats in python: a step-by-step guide (3 ways to do it)
Range of Floats in Python: A Step-by-Step Guide (3 Ways to Do It)
November 1, 2022 - To create a range of floats in Python, use a list comprehension. For example: [(x/10) for x in range(0, 5)] returns [0.0, 0.1, 0.2, 0.3, 0.4]
🌐
Python.org
discuss.python.org › ideas
Float contained in range - Ideas - Discussions on Python.org
September 7, 2023 - I have found that checking if a float number falls inside a range() doesn’t work, always giving False: >>> 1.0 in range (0, 2) True >>> 1.5 in range (0, 2) False I can understand that range() can only accept integer va…
🌐
Delft Stack
delftstack.com › home › howto › python › python range float
How to Get Range of Floating Numbers in Python | Delft Stack
February 2, 2024 - Therefore we will have to provide ... below example code demonstrates how to use the numpy.linspace() to generate the range of floats from 0 to 1 with the step equal to 0.1....
🌐
Sololearn
sololearn.com › en › Discuss › 818891 › solved-is-there-a-way-to-generate-a-float-range-list-with-step-
[SOLVED] Is there a way to generate a float range list with step ? | Sololearn: Learn to code for FREE!
I would like to know a way to generate a list like range(0,10) with float example range(pi,2pi,0.01) ... # You can use numpy arange() for that. It works similarly to range with (start, stop, step).
🌐
Reddit
reddit.com › r/learnpython › float numbers with range () function
r/learnpython on Reddit: float numbers with range () function
April 19, 2022 -

Hi, I have a section of code which takes a score from the user, typecasts it into a float value and saves it under the variable name "score". Then I have a line of code which reads:

while score not in range(0, 101):

However, when I input a float, I get an invalid input error message from python. I think it's because I can't use the range() function with floats. Is there a way around this? If anyone knows how to solve this, then your help would be greatly appreciated. Thank you in advance.

🌐
Guru99
guru99.com › home › python › python range() function: float, list, for loop examples
Python range() Function: Float, List, For loop Examples
August 12, 2024 - The start value is 15, the stop value is 5 and the step value is negative number i.e -1. With above inputs range() function will decrement the value from 15 onwards till it reaches the stop value , but here the difference is the last value will be stop + 1. Let us now work on the range() using floating-point numbers. ... In above example we have used stop value as 10.5. ... Traceback (most recent call last): File "python...
🌐
GeeksforGeeks
geeksforgeeks.org › python-range-for-float-numbers
Python – range() for Float Numbers | GeeksforGeeks
December 4, 2024 - If we want to create a range with float numbers, we can use a while loop. This loop will allow us to control the start, stop, and step values, including floats. ... # Initialize the starting, stopping, and stepping values start = 0 stop = 2 ...
🌐
YouTube
youtube.com › data science tutorials
Python 3 Programming Tutorial | How to create range values for float data type - YouTube
In this python 3 programming tutorial, I have talked about how you can create range of float values which is directly not possible using the range function. ...
Published   July 4, 2018
Views   1K
🌐
TutorialKart
tutorialkart.com › python › python-range › python-range-float
Python Range with Floating Point Numbers
January 17, 2021 - That is because range() function accepts only integers for start, stop and step arguments. So, to generate Range of floating point numbers, we have to either workaround range() function to generate sequence of floating point numbers.
🌐
Python Forum
python-forum.io › thread-17889.html
Inclusive (closed) range with float numbers
Hi I need to add range of numbers to some arrays used, but I see that range in Python is not for float numbers and is not inclusive, namely range is not including the last number. Seems that we need to use extra functions to complement the default ...
🌐
LabEx
labex.io › tutorials › python-how-to-use-range-with-non-integer-steps-462120
How to use range with non integer steps | LabEx
The built-in range() function in Python only supports integer steps, which limits its use with floating-point sequences.