classification
Title: Why does not range() support decimals?
Type: Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, larry, prasechen, rhettinger, steven.daprano, terry.reedy
Priority: normal Keywords:

Created on 2020-09-04 15:54 by prasechen, last changed 2020-09-12 03:20 by prasechen. This issue is now closed.

Messages (4)
msg376378 - (view) Author: chen-y0y0 (prasechen) Date: 2020-09-04 15:54
# I try:
>>> range(0,5,0.5)
# I hope it will (0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5). But...
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    range(0,5,0.5)
TypeError: 'float' object cannot be interpreted as an integer
msg376416 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-09-04 23:38
The original title was "Why does not range() support decimals?".  In general, such questions should be directed to question forums, such as https://mail.python.org/mailman/listinfo/python-list or stackoverflow.com.

This question has been answered on both, with alternatives, in particular on
https://stackoverflow.com/questions/477486/how-to-use-a-decimal-range-step-value
https://stackoverflow.com/questions/12403411/range-with-float-step-argument
and other duplicates.

I am leaving this open to add this frequent question to our FAQ, in particular at
https://docs.python.org/3/faq/programming.html#sequences-tuples-lists
with 'range' added.

This answer should include how to use range to get what people generally want.
msg376421 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-09-05 01:11
Generating a range of equally-spaced floats is tricky and the range builtin is not the right solution for this.

For numerical work, we often need to specify the number of steps, not the step size. For instance, in numeric integration, we often like to double the number of steps and avoid re-calculation.

We often need to skip either the start or end point, or both, or neither.

If you specify the step size, as range does, you can have off-by-one errors due to float rounding.

See here for a discussion and possible solution:

https://code.activestate.com/recipes/577878
msg376422 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-09-05 01:17
FWIW, the usual approach to the OP's problem is:

    >>> [x*0.5 for x in range(10)]
    [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

This technique generalizes to other sequences as well:

    >>> [x**2 for x in range(10)]
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

As Steven points out, numeric work typically requires something different and a bit more sophisticated.  The numpy package may be your best bet for this kind of work.
History
Date User Action Args
2020-09-12 03:20:20prasechensetstatus: open -> closed
resolution: not a bug
stage: needs patch -> resolved
2020-09-05 01:17:22rhettingersetnosy: + rhettinger
messages: + msg376422
2020-09-05 01:11:01steven.dapranosetnosy: + steven.daprano
messages: + msg376421
2020-09-04 23:38:49terry.reedysetassignee: docs@python
type: compile error ->
components: + Documentation, - Argument Clinic
versions: + Python 3.9, Python 3.10, - Python 3.5, Python 3.6, Python 3.7
nosy: + docs@python, terry.reedy

messages: + msg376416
stage: needs patch
2020-09-04 15:54:10prasechencreate