-1
t = range(10, 20, 1)

this generates a list of integer elements.

but I want to generate a list of float (double) elements with the same values like

t = range(10.0, 20.0, 1.0)

this doesn't work.

TypeError: range() integer end argument expected, got float.
TrW236
  • 367
  • 1
  • 5
  • 14

3 Answers3

6

It is not possible to do it with range. As a workaround, you may use map to type-cast the values to float as:

>>> map(float, range(10, 20, 1))
[10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0]
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
2

you can type cast int to float.

>>> [float(x) for x in range(10,20,1)]
[10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0]
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
1
[i*1.0 for i in range(10, 20, 1)]
Jacopo Lanzoni
  • 1,264
  • 2
  • 11
  • 25