Issue 13480: range exits loop without action when start is higher than end (original) (raw)

range has an odd behavior in which it assumes (regardless of start/end) that it should be counting up. Attempting something such as:

for i in range(10,0): print i

This loop simply runs through without doing anything, because start is larger than end.

I'm putting forward the proposition that when end is lower than start, range should count downwards rather than upwards.

Nope. If you want to count backward, use a negative step. Not doing anything if end is lower than start allows code to take advantage of "don't care" edge cases, just like 'abc'[4:] returning the empty string does. Range is often used in 'for' loops, so having the loop not execute if the computed end is less than the computed start is an intentional and important feature.