Arithmetic with Boolean values
Paul Rubin
no.email at nospam.invalid
Sat Aug 11 20:54:40 EDT 2012
John Ladasky <john_ladasky at sbcglobal.net> writes:
> I have gotten used to switching back and forth between Boolean algebra
> and numerical values. Python generally makes this quite easy.
Generally ugly though, at least to my tastes. "Explicit is better
than implicit" as the saying goes.
> If the length of the list L is odd, I want to process it once. If
> len(L) is even, I want to process it twice....
> for x in range(1 + not(len(L) % 2)):
If you really have to do something like that, I'd say
for x in range(1 + (len(L) & 1)):
or
for x in range(2 - len(L) % 2):
are simpler and avoid those bogus bool conversions. I'd prefer to
just say the intention:
for x in range(1 if len(L)%2==1 else 2):
> This provoked a SyntaxError.
"not" is a syntactic keyword and "1 + not" is syntactically invalid.
You could write "1 + (not ...)" as you discovered, but really, it's
hackish.
More information about the Python-list
mailing list