# Arithmetic with Boolean values

Discussion in 'Python' started by John Ladasky, Aug 11, 2012.

1. ### John LadaskyGuest

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.

John Ladasky, Aug 11, 2012

2. ### Chris AngelicoGuest

On Sun, Aug 12, 2012 at 8:30 AM, John Ladasky
<> 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

Chris Angelico, Aug 12, 2012

3. ### Chris RebertGuest

On Sat, Aug 11, 2012 at 3:30 PM, John Ladasky
<> wrote:
<snip>
> for x in range(1 + not(len(L) % 2)):
> # Do stuff
>
> This provoked a SyntaxError. I investigated this further with my interpreter (ipython).

<snip>
> 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

<snip>
> 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)
>>> len("abc")

3
>>> # functions are first-class entities in Python, so we can reference them
>>> len

<built-in function len>
>>> not(1)

False
>>> # but `not` isn't a function
>>> not

File "<stdin>", line 1
not
^
SyntaxError: invalid syntax
>>> # hence why we don't need to call it
>>> not 1

False

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.

Chris Rebert, Aug 12, 2012
4. ### Terry ReedyGuest

On 8/11/2012 7:13 PM, Chris Angelico wrote:
> On Sun, Aug 12, 2012 at 8:30 AM, John Ladasky
> <> 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.

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)
>
> 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).

--
Terry Jan Reedy

Terry Reedy, Aug 12, 2012
5. ### Chris AngelicoGuest

On Sun, Aug 12, 2012 at 10:25 AM, Terry Reedy <> wrote:
> On 8/11/2012 7:13 PM, Chris Angelico wrote:
>> 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.
> (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

Chris Angelico, Aug 12, 2012
6. ### MRABGuest

On 11/08/2012 23:30, John Ladasky wrote:
> 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

MRAB, Aug 12, 2012
7. ### Paul RubinGuest

John Ladasky <> 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.

Paul Rubin, Aug 12, 2012
8. ### Steven D'ApranoGuest

On Sat, 11 Aug 2012 17:54:40 -0700, Paul Rubin wrote:

> John Ladasky <> writes:

[...]
>> 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)):

[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.

--
Steven

Steven D'Aprano, Aug 12, 2012
9. ### Roy SmithGuest

In article <502791ea\$0\$29978\$c3e8da3\$>,
Steven D'Aprano <> wrote:

> 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.

Roy Smith, Aug 12, 2012
10. ### Steven D'ApranoGuest

On Sun, 12 Aug 2012 07:40:30 -0400, Roy Smith wrote:

> In article <502791ea\$0\$29978\$c3e8da3\$>,
> Steven D'Aprano <> wrote:
>
>> 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.

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):
...

--
Steven

Steven D'Aprano, Aug 12, 2012
11. ### Paul RubinGuest

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

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

Paul Rubin, Aug 12, 2012
12. ### Bernd NawothnigGuest

On 2012-08-12, Paul Rubin wrote:
>> which can be simplified to:
>> for x in range(len(L)//2 + len(L)%2):

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

nice solution.

Bernd

--
"Die Antisemiten vergeben es den Juden nicht, dass die Juden Geist
haben - und Geld." [Friedrich Nietzsche]

Bernd Nawothnig, Aug 12, 2012
13. ### Mark LawrenceGuest

On 12/08/2012 17:59, Paul Rubin wrote:
>> which can be simplified to:
>> for x in range(len(L)//2 + len(L)%2):

>
> 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..."?

--
Cheers.

Mark Lawrence.

Mark Lawrence, Aug 12, 2012
14. ### Roy SmithGuest

In article <>,
Mark Lawrence <> wrote:

> On 12/08/2012 17:59, Paul Rubin wrote:
> >> which can be simplified to:
> >> for x in range(len(L)//2 + len(L)%2):

> >
> > 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..."?

You, apparently.

Roy Smith, Aug 12, 2012
15. ### Gene HeskettGuest

On Sunday 12 August 2012 20:27:13 Alister did opine:

> On Sun, 12 Aug 2012 19:20:26 +0100, Mark Lawrence wrote:
> > On 12/08/2012 17:59, Paul Rubin wrote:
> >>> which can be simplified to:
> >>
> >>> for x in range(len(L)//2 + len(L)%2):
> >> 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..."?

>
> 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

Gene Heskett, Aug 13, 2012
16. ### Hans MulderGuest

On 12/08/12 22:13:20, Alister wrote:
> On Sun, 12 Aug 2012 19:20:26 +0100, Mark Lawrence wrote:
>
>> On 12/08/2012 17:59, Paul Rubin wrote:
>>>> which can be simplified to:
>>>> for x in range(len(L)//2 + len(L)%2):
>>>
>>> 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..."?

>
> Five is right out

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

-- HansM

Hans Mulder, Aug 14, 2012

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.