[Tutor] ask

Kent Johnson kent37 at tds.net
Wed Feb 24 14:45:17 CET 2010


On Tue, Feb 23, 2010 at 10:58 PM, Shurui Liu (Aaron Liu)
<shurui91 at gmail.com> wrote:
> This time is not my assignment, I promise.
>
> In python, when we want to list numbers, we use the command "range", like,
> if we want to list integer from 0 to 9, we can write: range(10); if we want
> to list integer from 10 to 29, we can write: range(10,30). I was going to
> show a list of number from 1.0 to 1.9, and I did this in the same way as
> integer: range(1.0,2.0,0.1), but it doesn't work. Can you help me? Thank
> you!

Right, range() only works with integer arguments. You can use a list
comprehension to convert to floats:

In [2]: print [ x/10.0 for x in range(10, 20) ]
[1.0, 1.1000000000000001, 1.2, 1.3, 1.3999999999999999, 1.5,
1.6000000000000001, 1.7, 1.8, 1.8999999999999999]

The rounding errors are to be expected, see this link for an explanation:
http://docs.python.org/tutorial/floatingpoint.html

Kent


More information about the Tutor mailing list