Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26932
| Date | 2012-08-12 01:36 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Arithmetic with Boolean values |
| References | <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3203.1344731811.4697.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Arithmetic with Boolean values John Ladasky <john_ladasky@sbcglobal.net> - 2012-08-11 15:30 -0700
Re: Arithmetic with Boolean values Chris Angelico <rosuav@gmail.com> - 2012-08-12 09:13 +1000
Re: Arithmetic with Boolean values Chris Rebert <clp2@rebertia.com> - 2012-08-11 16:53 -0700
Re: Arithmetic with Boolean values Terry Reedy <tjreedy@udel.edu> - 2012-08-11 20:25 -0400
Re: Arithmetic with Boolean values Chris Angelico <rosuav@gmail.com> - 2012-08-12 10:31 +1000
Re: Arithmetic with Boolean values MRAB <python@mrabarnett.plus.com> - 2012-08-12 01:36 +0100
Re: Arithmetic with Boolean values Paul Rubin <no.email@nospam.invalid> - 2012-08-11 17:54 -0700
Re: Arithmetic with Boolean values Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-12 11:22 +0000
Re: Arithmetic with Boolean values Roy Smith <roy@panix.com> - 2012-08-12 07:40 -0400
Re: Arithmetic with Boolean values Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-12 14:06 +0000
Re: Arithmetic with Boolean values Paul Rubin <no.email@nospam.invalid> - 2012-08-12 09:59 -0700
Re: Arithmetic with Boolean values Bernd Nawothnig <Bernd.Nawothnig@t-online.de> - 2012-08-12 19:21 +0200
Re: Arithmetic with Boolean values Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-12 19:20 +0100
Re: Arithmetic with Boolean values Roy Smith <roy@panix.com> - 2012-08-12 14:45 -0400
Re: Arithmetic with Boolean values Alister <alister.ware@ntlworld.com> - 2012-08-12 20:13 +0000
Re: Arithmetic with Boolean values Gene Heskett <gheskett@wdtv.com> - 2012-08-12 20:29 -0400
Re: Arithmetic with Boolean values Hans Mulder <hansmu@xs4all.nl> - 2012-08-14 18:32 +0200
csiph-web