Python range function

ID : 274382

viewed : 26

Tags : pythonpython-2.7rangepython





Top 5 Answer for Python range function

vote vote

95

Python2.x:

for idx in range(0, int(100 / 0.5)):      print 0.5 * idx       

outputs:

0.0

0.5

1.0

1.5

..

99.0

99.5


Numpy:

numpy.arange would also do the trick.

numpy.arange(0, 100, 0.5) 
vote vote

82

If you have numpy, here are two ways to do it:

numpy.arange(0, 100, 0.5)  numpy.linspace(0, 100, 200, endpoint=False) 
vote vote

76

You have to use integer steps for range() and xrange(). That's why your 0.5 step gets internally converted to 0 and you get that error. Try for i in [j / 2.0 for j in xrange(100 * 2)]:

vote vote

67

You'll have to either create the loop manually, or define your own custom range function. The built-in requires an integer step value.

vote vote

59

for x in map(lambda i: i * 0.5, range(0,200)):   #Do something with x 

Top 3 video Explaining Python range function







Related QUESTION?