Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; 'interpreter': 0.04; 'preference': 0.05; 'test,': 0.05; 'false.': 0.07; 'function,': 0.07; 'python': 0.09; '"("': 0.09; '"+"': 0.09; '[0,': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'atom': 0.09; 'identifier': 0.09; 'literal': 0.09; '"%"': 0.16; '")"': 0.16; '"*"': 0.16; '"-"': 0.16; '"/"': 0.16; '"//"': 0.16; '"not"': 0.16; '"~"': 0.16; '::=': 0.16; '["**"': 0.16; '[","]': 0.16; '[10]': 0.16; '[4]:': 0.16; 'a_expr': 0.16; 'attributeref': 0.16; 'boolean': 0.16; 'dict_display': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'list_display': 0.16; 'm_expr': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'subject:values': 0.16; 'syntax.': 0.16; 'syntaxerror:': 0.16; 'twice.': 0.16; 'u_expr': 0.16; 'wrote:': 0.17; 'example.': 0.17; 'numerical': 0.17; 'are:': 0.20; '"",': 0.22; 'logical': 0.22; "i'd": 0.22; 'this:': 0.23; "i've": 0.23; 'pass': 0.25; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'rules': 0.27; 'see,': 0.27; "doesn't": 0.28; 'fine': 0.28; 'once.': 0.29; 'received:192.168.1.3': 0.29; "i'm": 0.29; 'function': 0.30; 'primary': 0.30; 'stuff': 0.30; 'code': 0.31; 'file': 0.32; 'generally': 0.32; 'getting': 0.33; 'true.': 0.33; 'values.': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'program,': 0.34; 'list': 0.35; 'false': 0.35; 'follows:': 0.35; 'returning': 0.35; 'so,': 0.35; 'something': 0.35; 'list.': 0.35; 'add': 0.36; 'but': 0.36; 'subject:with': 0.36; 'test': 0.36; 'why': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'john': 0.60; 'further': 0.61; 'back': 0.62; 'between': 0.63; 'different': 0.63; 'here': 0.65; 'header:Reply- To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'power': 0.74; 'forth': 0.75; 'gotten': 0.75; 'investigated': 0.84; 'matters.': 0.84; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=W6e6pGqk c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=DKcI9XZsuF4A:10 a=GmeNTM2__I0A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=TMox7NzayRFfwR3W7-IA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sun, 12 Aug 2012 01:36:45 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Arithmetic with Boolean values References: <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> In-Reply-To: <58f60e60-1dc1-4265-a601-693d11b4bcec@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344731811 news.xs4all.nl 6913 [2001:888:2000:d::a6]:33622 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26932 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 "", 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