On Fri, Sep 20, 2019 at 5:16 AM Cecil Westerhof <ce...@decebal.nl> wrote:
>
> Paul Rubin <no.email@nospam.invalid> writes:
>
> >     Python 3.7.3 (default, Apr  3 2019, 05:39:12)
> >     Type "help", "copyright", "credits" or "license" for more information.
> >     >>> a = range(10)
> >     >>> b = reversed(a)
> >     >>> sum(a) == sum(b)
> >     True
> >     >>> sum(b) == sum(a)
> >     False
>
> Why does this happen?
>
> By the way, when you change the last statement to:
>     sum(a) == sum(b)
>
> you also get False.

>>> sum(range(10)) == sum(reversed(range(10)))
True

If you actually want a reversed range, use slice notation instead of
the reversed() function, which is more parallel to iter().

>>> a = range(10)
>>> b = a[::-1]
>>> sum(a) == sum(b)
True
>>> sum(b) == sum(a)
True

Now you actually have a range that runs the other direction, instead
of an iterator that runs through the range backwards.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to