Albert Albert - 1 year ago 114
Python Question

Range with step of type float

The documentation basically says that

must behave exactly as this implementation (for positive

def range(start, stop, step):
x = start
while True:
if x >= stop: return
yield x
x += step

It also says that its arguments must be integers. Why is that? Isn't that definition also perfectly valid if step is a float?

In my case, I am esp. needing a
function which accepts a float type as its
argument. Is there any in Python or do I need to implement my own?

More specific: How would I translate this C code directly to Python in a nice way (i.e. not just doing it via a
-loop manually):

for(float x = 0; x < 10; x += 0.5f) { /* ... */ }

Answer Source

You could use numpy.arange.

EDIT: The docs prefer numpy.linspace. Thanks @Droogans for noticing =)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download