# Arithmetic with Boolean values

J

I have gotten used to switching back and forth between Boolean algebra and numerical values. Python generally makes this quite easy. I just found a case that surprises me.

Here is what I want to accomplish: I want to process a list. 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. I thought I would set up a loop as follows:

for x in range(1 + not(len(L) % 2)):
# Do stuff

This provoked a SyntaxError. I investigated this further with my interpreter (ipython).

In [1]: L = range(5)

In [2]: L
Out[2]: [0, 1, 2, 3, 4]

In [3]: len(L)
Out[3]: 5

In [4]: len(L) % 2
Out[4]: 1

In [5]: not(1)
Out[5]: False

In [6]: not(len(L) % 2)
Out[6]: False

In [7]: 1 + not(len(L) % 2)
------------------------------------------------------------
File "<ipython console>", line 1
1 + not(len(L) % 2)
^
SyntaxError: invalid syntax

So as you can see, every thing is working fine until I attempt to add 1 andFalse. However:

In [8]: 0 == False
Out[8]: True

In [9]: 1 == True
Out[9]: True

So, 0 and False do pass an equivalency test, as do 1 and True. Furthermore:

In [10]: 1 + (len(L) % 2 == 0)
Out[10]: 1

Why is using a logical "not" function, as shown in [7], returning a different result than the test for equivalency as shown in [10]?

Of course I'm just going to use [10] in my program, but I'd like to understand the reason that I'm getting that SyntaxError. I've been reading Pythonstyle guides, and at least one of them states a preference for using the "not" syntax over the "== 0" syntax.

I'm using Python 2.7, in case it matters.

C

#### Chris Angelico

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

C

#### Chris Rebert

for x in range(1 + not(len(L) % 2)):
# Do stuff

This provoked a SyntaxError. I investigated this further with my interpreter (ipython).
In [5]: not(1)
Out[5]: False

In [6]: not(len(L) % 2)
Out[6]: False

In [7]: 1 + not(len(L) % 2)
------------------------------------------------------------
File "<ipython console>", line 1
1 + not(len(L) % 2)
^
SyntaxError: invalid syntax
Why is using a logical "not" function, as shown in [7], returning a different result than the test for equivalency as shown in [10]?

Note that, in Python, `not` is an unary operator (it's a language
keyword in fact), as opposed to a function:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)File "<stdin>", line 1
not
^
SyntaxError: invalid syntaxFalse

The parentheses in `not(foo)` are just grouping the `foo`
subexpression; they're not indicating a function call. Therefore, in
idiomatic Python, such parentheses are omitted whenever possible
(which is the majority of the time), and when they are absolutely
necessary, a space is included before the opening paren, because we're
not making a function call.

Thus, you're simply running into an operator precedence issue, which,
as Chris A. explained, can be remedied by adding grouping parens
around the entire `not` expression; e.g. `(not foo)`

Cheers,
Chris R.

T

#### Terry Reedy

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.

I think not. It is lower precedence than all arithmetic operators. The
following is worth knowing about and occasionally reviewing.
http://docs.python.org/py3k/reference/expressions.html#summary

() around % is not needed; not len(L) % 2 works same. So parser sees

1 + not len(L) % 2

with + given higher precedence than not, So it parses as (1 + not) and
croaks, as indicated by caret. (We humans see that that is impossible
and may boost the precedence in context.)
1 + (not(len(L) % 2))

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

C

#### Chris Angelico

I think not. It is lower precedence than all arithmetic operators.
(We humans see that that is impossible and
may boost the precedence in context.)

Ah, I stand corrected. And once again, I kinda expected Python to
follow the rules of my mental parser

Anyway, point stands that parens will fix the issue.

ChrisA

M

#### MRAB

I have gotten used to switching back and forth between Boolean algebra and numerical values. Python generally makes this quite easy. I just found a case that surprises me.

Here is what I want to accomplish: I want to process a list. 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. I thought I would set up a loop as follows:

for x in range(1 + not(len(L) % 2)):
# Do stuff

This provoked a SyntaxError. I investigated this further with my interpreter (ipython).

In [1]: L = range(5)

In [2]: L
Out[2]: [0, 1, 2, 3, 4]

In [3]: len(L)
Out[3]: 5

In [4]: len(L) % 2
Out[4]: 1

In [5]: not(1)
Out[5]: False

In [6]: not(len(L) % 2)
Out[6]: False

In [7]: 1 + not(len(L) % 2)
------------------------------------------------------------
File "<ipython console>", line 1
1 + not(len(L) % 2)
^
SyntaxError: invalid syntax

So as you can see, every thing is working fine until I attempt to add 1 and False. However:

In [8]: 0 == False
Out[8]: True

In [9]: 1 == True
Out[9]: True

So, 0 and False do pass an equivalency test, as do 1 and True. Furthermore:

In [10]: 1 + (len(L) % 2 == 0)
Out[10]: 1

Why is using a logical "not" function, as shown in [7], returning a different result than the test for equivalency as shown in [10]?

Of course I'm just going to use [10] in my program, but I'd like to understand the reason that I'm getting that SyntaxError. I've been reading Python style guides, and at least one of them states a preference for using the "not" syntax over the "== 0" syntax.

I'm using Python 2.7, in case it matters.
I think the problem is that "not" isn't a function as such - it doesn't
require parentheses, for example.

The relevant syntax rules are:

a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr

m_expr ::= u_expr | m_expr "*" u_expr | m_expr "//" u_expr | m_expr "/"
u_expr | m_expr "%" u_expr

u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr

power ::= primary ["**" u_expr]

primary ::= atom | attributeref | subscription | slicing | call

atom ::= identifier | literal | enclosure

enclosure ::= parenth_form | list_display | dict_display | set_display |
generator_expression | yield_atom

call ::= primary "(" [argument_list [","] | comprehension] ")"

In order for your code to work I think we would need to have something
like this:

primary ::= atom | attributeref | subscription | slicing | call | not_expr

not_expr ::= "not" parenth_form

P

#### Paul Rubin

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.

S

#### Steven D'Aprano

If you really have to do something like that, I'd say

for x in range(1 + (len(L) & 1)):
[snip]

I'd simplify it even more:

for x in (0,) if len(L)%2 else (0, 1):
...

which is even more explicit and simpler to read even though it is longer.

R

#### Roy Smith

Steven D'Aprano said:
for x in (0,) if len(L)%2 else (0, 1):
...

which is even more explicit and simpler to read even though it is longer.

Ugh.

do_stuff()
if len(L) % 2 == 0:
do_stuff() # reprocess even-length list

Sure, it's 3 lines instead of one, but dead-obvious what the intention
is. I might even go for:

if len(L) % 2:
do_stuff()
else:
do_stuff()
do_stuff()

There's two problems with all the looping suggestions people have given.
One is that the computation of whether you need to do it once or twice
is messy. But, but bigger issue is you're trying to warp what's
fundamentally a boolean value into a looping construct. That's a
cognitive mismatch.

S

#### Steven D'Aprano

Ugh.

do_stuff()
if len(L) % 2 == 0:
do_stuff() # reprocess even-length list

Sure, it's 3 lines instead of one, but dead-obvious what the intention
is.

Well, sure, for that specific case that would work too. Using a for-loop
to do something once is a little icky. But only a little.

Also, it scales to situations like "repeat 37 times when even, or 82
times when odd" (or any other values).

There's two problems with all the looping suggestions people have given.
One is that the computation of whether you need to do it once or twice
is messy. But, but bigger issue is you're trying to warp what's
fundamentally a boolean value into a looping construct. That's a
cognitive mismatch.

Really? You've never used a while loop?

while not finished:
do_something()

There's nothing wrong with having a for-loop which iterates over a
computed set of values:

if len(L)%2:
items = range(len(L)//2 + 1)
else:
items = range(len(L)//2)
for x in items:
...

which can be simplified to:

for x in range(len(L)//2 + len(L)%2):
...

P

#### Paul Rubin

which can be simplified to:
for x in range(len(L)//2 + len(L)%2):

for x in range(sum(divmod(len(L), 2))): ...

B

M

#### Mark Lawrence

for x in range(sum(divmod(len(L), 2))): ...

So who's going to be first in with "and thou shalt not count to 4..."?

R

#### Roy Smith

Mark Lawrence said:
So who's going to be first in with "and thou shalt not count to 4..."?

You, apparently.

G

#### Gene Heskett

Five is right out

Can some smart ass (like me) suggest 69!

If it doesn't get summarily tossed, it could take a week or so to run.

Cheers, Gene
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
My web page: <http://coyoteden.dyndns-free.com:85/gene> is up!
e-credibility: the non-guaranteeable likelihood that the electronic data
you're seeing is genuine rather than somebody's made-up crap.
-- Karl Lehenbauer

H

#### Hans Mulder

Five is right out

Neither count thou two, excepting that thou then proceed to three

-- HansM