This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: range exits loop without action when start is higher than end
Type: behavior Stage: resolved
Components: None Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Asa.Dawson, r.david.murray
Priority: normal Keywords:

Created on 2011-11-25 14:51 by Asa.Dawson, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg148315 - (view) Author: Asa Dawson (Asa.Dawson) Date: 2011-11-25 14:51
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.
msg148316 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-11-25 14:59
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.
History
Date User Action Args
2022-04-11 14:57:24adminsetgithub: 57689
2011-11-25 14:59:31r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg148316

resolution: not a bug
stage: resolved
2011-11-25 14:51:34Asa.Dawsoncreate