Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'value,': 0.04; 'string.': 0.05; 'detect': 0.07; "subject:' ": 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runs': 0.10; 'python': 0.11; 'thread': 0.14; '"and"': 0.16; '"or"': 0.16; 'at.': 0.16; 'finds': 0.16; 'kern': 0.16; 'nick': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'track.': 0.16; 'underlying': 0.16; 'year)': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'seems': 0.21; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'interpret': 0.24; 'values': 0.27; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'character': 0.29; 'returned': 0.30; 'robert': 0.30; 'this.': 0.32; 'know.': 0.32; 'another': 0.32; 'url:python': 0.33; 'subject:with': 0.35; 'but': 0.35; 'replies': 0.36; 'returning': 0.36; 'doing': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'expression': 0.60; 'matter': 0.61; 'first': 0.61; 'our': 0.64; 'within': 0.65; 'world': 0.66; 'here': 0.66; 'between': 0.67; 'believe': 0.68; 'eco': 0.84; 'irrelevant': 0.84; 'terrible': 0.84; 'url:reference': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: Eval of expr with 'or' and 'and' within Date: Fri, 14 Jun 2013 11:14:16 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 213.1.240.226 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371204867 news.xs4all.nl 15977 [2001:888:2000:d::a6]:58741 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48109 On 2013-06-14 10:50, Nick the Gr33k wrote: > I started another thread because the last one was !@#$'ed up by irrelevant > replies and was difficult to jeep track. > > >>> name="abcd" > >>> month="efgh" > >>> year="ijkl" > > >>> print(name or month or year) > abcd > > Can understand that, it takes the first string out of the 3 strings that has a > truthy value. > > >>> print("k" in (name and month and year)) > True > > No clue. since the expression in parenthesis returns 'abcd' how can 'k' > contained within 'abcd' ? > > >>> print(name and month and year) > ijkl > > Seems here is returning the last string out of 3 strings, but have no clue why > Python doing this. > > >>> print("k" in (name and month and year)) > True > >>> > > yes, since expression returns 'ijkl', then the in operator can detect the 'k' > character within the returned string. > > This is all iw ant to know. This is all you need to read: http://docs.python.org/2/reference/expressions.html#boolean-operations Note the difference between how "or" and "and" each short-circuit. That is why the (name or month or year) returns the first truthy value while (name and month and year) returns the last truthy value. When "or" finds the first truthy value, it can stop looking since the whole expression must be truthy no matter what the values are after it. "and" cannot stop looking until it finds a falsy value or runs out of values to look at. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco