Arithmetic with Boolean values

Chris Angelico rosuav at gmail.com
Sat Aug 11 19:13:15 EDT 2012


On Sun, Aug 12, 2012 at 8:30 AM, John Ladasky
<john_ladasky at sbcglobal.net> wrote:
> In [7]: 1 + not(len(L) % 2)
> ------------------------------------------------------------
>    File "<ipython console>", line 1
>      1 + not(len(L) % 2)
>            ^
> SyntaxError: invalid syntax

This appears to be a limitation of the parser; it's trying to
interpret "not" as a binary operator.

1 + (not(len(L) % 2))

Works just fine with parentheses to enforce the correct interpretation.

This also works in Python 3.2, fwiw (except that you need
list(range(5)) to create the sample list).

ChrisA



More information about the Python-list mailing list