Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #48268

Re: Eval of expr with 'or' and 'and' within

From Nick the Gr33k <support@superhost.gr>
Newsgroups comp.lang.python
Subject Re: Eval of expr with 'or' and 'and' within
Date 2013-06-15 11:20 +0300
Organization National Technical University of Athens, Greece
Message-ID <kph83o$29li$5@news.ntua.gr> (permalink)
References <kpep0v$spl$8@news.ntua.gr> <mailman.3351.1371257115.3114.python-list@python.org>

Show all headers | View raw


On 15/6/2013 3:14 πμ, Cameron Simpson wrote:
> On 14Jun2013 12:50, Nikos as SuperHost Support <support@superhost.gr> 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' ?
>
> Did you print the result of "name and month and year"? It is the
> _last_ value (if true at all).  You used "or" in the first example
> and "and" in the second.

okey, lets see it again:

 >>> print (name or month or year)
abcd
 >>>

Yes, 'k' isn't contained in the result string 'abcd'

 >>> print (name and month and year)
hijk
 >>> print( "k" in (name and month and year) )
True

Yes they work as expected, i was mistaken, sorry.


>
> | >>> 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.
>
> To evaluate an "and" it must test all of them to be true, and it
> keeps the last value tested.  (Or False, of course, if they are not
> all true, in which case Python stops testing at the first False).

Yes, i know it behaves like that, the question is why:

As "Nobody" explained to me, the reason is that Python expressions 
results back the argument that determined the evaluation of the Boolean 
expression, which in turn can be a truthy or a falsey used in 'or' or 
'and' respectively.

Returning a truthy value equals True
returning a falsey value equals False

so it all boils down to the Booleans type of values True or False.


> But for what you are doing, "and" and "or" are not good operations.
>
> Something like:
>
>    "k" in (name+month+year)
>
> or
>
>    "k" in name or "k" in month or "k" in year

Used to wrote it myself like the latter but needed a more compact way of 
writing it for clarity so i used the former.

but those 2 gives the same results back

"k" in (name+month+year) == "k" in (name and month and year)
True

so both seem to work as expected.
-- 
What is now proved was at first only imagined!

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-14 12:50 +0300
  Re: Eval of expr with 'or' and 'and' within Fábio Santos <fabiosantosart@gmail.com> - 2013-06-14 11:03 +0100
  Re: Eval of expr with 'or' and 'and' within Robert Kern <robert.kern@gmail.com> - 2013-06-14 11:14 +0100
    Re: Eval of expr with 'or' and 'and' within Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-14 16:09 +0000
      Re: Eval of expr with 'or' and 'and' within rusi <rustompmody@gmail.com> - 2013-06-14 09:21 -0700
      Re: Eval of expr with 'or' and 'and' within Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-06-14 20:03 +0200
        Re: Eval of expr with 'or' and 'and' within rusi <rustompmody@gmail.com> - 2013-06-14 11:37 -0700
          Re: Eval of expr with 'or' and 'and' within Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-14 19:47 +0100
      Re: Eval of expr with 'or' and 'and' within alex23 <wuwei23@gmail.com> - 2013-06-14 23:50 -0700
        Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-15 10:04 +0300
          Re: Eval of expr with 'or' and 'and' within Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-15 07:49 +0000
            Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-15 10:59 +0300
  Re: Eval of expr with 'or' and 'and' within Grant Edwards <invalid@invalid.invalid> - 2013-06-14 14:49 +0000
    Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-14 18:16 +0300
      Re: Eval of expr with 'or' and 'and' within Nobody <nobody@nowhere.com> - 2013-06-14 17:42 +0100
        Re: Eval of expr with 'or' and 'and' within Grant Edwards <invalid@invalid.invalid> - 2013-06-14 19:30 +0000
          Re: Eval of expr with 'or' and 'and' within Nobody <nobody@nowhere.com> - 2013-06-15 00:02 +0100
        Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-15 10:57 +0300
  Re: Eval of expr with 'or' and 'and' within Michael Torrie <torriem@gmail.com> - 2013-06-14 10:29 -0600
    Re: Eval of expr with 'or' and 'and' within Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-14 16:49 +0000
      Re: Eval of expr with 'or' and 'and' within Michael Torrie <torriem@gmail.com> - 2013-06-14 11:28 -0600
      Re: Eval of expr with 'or' and 'and' within MRAB <python@mrabarnett.plus.com> - 2013-06-14 18:49 +0100
      Re: Eval of expr with 'or' and 'and' within Chris Angelico <rosuav@gmail.com> - 2013-06-15 03:56 +1000
        Re: Eval of expr with 'or' and 'and' within Grant Edwards <invalid@invalid.invalid> - 2013-06-14 19:33 +0000
          Re: Eval of expr with 'or' and 'and' within Chris Angelico <rosuav@gmail.com> - 2013-06-15 09:56 +1000
        Re: Eval of expr with 'or' and 'and' within Nobody <nobody@nowhere.com> - 2013-06-15 00:09 +0100
          Re: Eval of expr with 'or' and 'and' within Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-15 01:34 +0000
            Re: Eval of expr with 'or' and 'and' within Cameron Simpson <cs@zip.com.au> - 2013-06-15 12:03 +1000
              NANs [was Re: Eval of expr with 'or' and 'and' within] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-15 04:22 +0000
            Re: Eval of expr with 'or' and 'and' within Chris Angelico <rosuav@gmail.com> - 2013-06-15 12:21 +1000
      Re: Eval of expr with 'or' and 'and' within Nobody <nobody@nowhere.com> - 2013-06-15 00:06 +0100
        Re: Eval of expr with 'or' and 'and' within MRAB <python@mrabarnett.plus.com> - 2013-06-15 01:26 +0100
  Re: Eval of expr with 'or' and 'and' within Benjamin Kaplan <benjamin.kaplan@case.edu> - 2013-06-14 09:47 -0700
    Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-14 20:01 +0300
      Re: Eval of expr with 'or' and 'and' within Robert Kern <robert.kern@gmail.com> - 2013-06-14 18:27 +0100
      Re: Eval of expr with 'or' and 'and' within Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-14 22:05 +0300
      Re: Eval of expr with 'or' and 'and' within Grant Edwards <invalid@invalid.invalid> - 2013-06-14 19:36 +0000
  Re: Eval of expr with 'or' and 'and' within Cameron Simpson <cs@zip.com.au> - 2013-06-15 10:14 +1000
    Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-15 11:20 +0300
      Re: Eval of expr with 'or' and 'and' within Lele Gaifax <lele@metapensiero.it> - 2013-06-15 11:48 +0200
        Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-15 16:24 +0300
        Re: Eval of expr with 'or' and 'and' within Nick the Gr33k <support@superhost.gr> - 2013-06-15 16:28 +0300

csiph-web