# PEP 276 Simple Iterator for ints (fwd)

Christopher A. Craig com-nospam at ccraig.org
Tue Dec 11 14:26:23 CET 2001

```David Eppstein <eppstein at ics.uci.edu> writes:

> For example, suppose you want to loop over the set of integers i satisfying
> x <= i < y (the usual half-open interval), but you need them in reverse
> order, and x and y may be non-integer.  As far as I can tell, the "one
> right way" of doing this is
>     import math
>     for i in range(int(math.ceil(y-1)),int(math.ceil(x-1)),-1): ...

How about:

i = y
while i > x:
print i
i-=1

That looks quite clear to me.  A heck of a lot clearer than

for i in y > i >= x: print i

Which to me does not specify the step size for floats.  (If y=5 and
x=0, shouldn't that print 3.1415926 at some point?)

Python's "for" loops were designed to iterate over sequence objects.
>From the way range was designed it seems fairly obvious that even it
was designed, not for iteration over mathematic sequences, but
traversing python lists by index instead of value.

Even with generators and iterators, I can't imagine why you would want
to use a for loop to iterate over "a set of integers i satisfying x <=
i < y in reverse order where x and y may be non-integer".  (Or over
floats at all for that matter) This, and its whole class of similar
mathematical sequences scream "while" at me.

--
Christopher A. Craig <com-nospam at ccraig.org>

```

More information about the Python-list mailing list