Arithmetic sequences in Python

Paul Rubin http
Mon Jan 16 16:35:48 EST 2006


aleax at mail.comcast.net (Alex Martelli) writes:
> > Oh come on, [5,4,..0] is much easier to read than range(5,-1,-1).
> 
> But not easier than reversed(range(6)) 

Heh, I like that, and reversed(xrange(6)) appears to do the right
thing too.  I didn't know about __reversed__ before.

> [[the 5 in one of the two
> expressions in your sentence has to be an offbyone;-)]]

Are you sure?  I could easily be missing something, since it's easy
to be offbyone with this stuff, but when I try it I get:

    Python 2.4.1 (#1, May 16 2005, 15:19:29)
    [GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> range(5,-1,-1)
    [5, 4, 3, 2, 1, 0]
    >>>

and (skipping the ascii art banner):

  Hugs 98: Based on the Haskell 98 standard
  Haskell 98 mode: Restart with command line option -98 to enable extensions
  Type :? for help
  Hugs.Base> [5,4..0]
  [5,4,3,2,1,0]
  Hugs.Base>

which is equivalent.  (Of course, having to use 6 instead of 5 in
the range(...) version invites an offbyone error).



More information about the Python-list mailing list