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


Groups > comp.lang.python > #48106 > unrolled thread

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

Started byNick the Gr33k <support@superhost.gr>
First post2013-06-14 12:50 +0300
Last post2013-06-15 16:28 +0300
Articles 20 on this page of 42 — 18 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 1 of 3  [1] 2 3  Next page →


#48106 — Eval of expr with 'or' and 'and' within

FromNick the Gr33k <support@superhost.gr>
Date2013-06-14 12:50 +0300
SubjectEval of expr with 'or' and 'and' within
Message-ID<kpep0v$spl$8@news.ntua.gr>
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.
-- 
What is now proved was at first only imagined!

[toc] | [next] | [standalone]


#48108

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-06-14 11:03 +0100
Message-ID<mailman.3285.1371204248.3114.python-list@python.org>
In reply to#48106

[Multipart message — attachments visible in raw view] — view raw

On 14 Jun 2013 10:59, "Nick the Gr33k" <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' ?
>
> >>> 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.

You have been explained with both words, links and examples and now you
dismiss the previous post because it was "difficult to jeep track"?

Anyway. Search for "short circuit logic". Short circuit logic is why python
does it that way.

[toc] | [prev] | [next] | [standalone]


#48109

FromRobert Kern <robert.kern@gmail.com>
Date2013-06-14 11:14 +0100
Message-ID<mailman.3288.1371204867.3114.python-list@python.org>
In reply to#48106
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

[toc] | [prev] | [next] | [standalone]


#48170

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-14 16:09 +0000
Message-ID<51bb4030$0$29997$c3e8da3$5496439d@news.astraweb.com>
In reply to#48109
On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:

> On 2013-06-14 10:50, Nick the Gr33k wrote:
[snip question]
>> This is all iw ant to know.
> 
> This is all you need to read:
> 
>    http://docs.python.org/2/reference/expressions.html#boolean-
operations


Thank you Robert for contributing a helpful response.

To everyone else... I know that Nikos' posts are draining. Sometimes he 
brings me to the brink of despair too. But if you aren't part of the 
solution, you are part of the problem: writing short-tempered, insulting 
posts after short-tempered, insulting post doesn't teach him, it just 
adds to EVERYBODY'S frustration with this never-ending serious of threads.

Please keep the snarky comments offlist. Contribute something helpful if 
you like, mute the thread or killfile Nikos if you must, but adding to 
the volume of unproductive junk doesn't do anyone any favours.

Thank you.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#48174

Fromrusi <rustompmody@gmail.com>
Date2013-06-14 09:21 -0700
Message-ID<57f2ba71-3275-433d-9b14-7363d21f96b9@k1g2000pbl.googlegroups.com>
In reply to#48170
On Jun 14, 9:09 pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:
> > On 2013-06-14 10:50, Nick the Gr33k wrote:
> [snip question]
> >> This is all iw ant to know.
>
> > This is all you need to read:
>
> >    http://docs.python.org/2/reference/expressions.html#boolean-
>
> operations
>
> Thank you Robert for contributing a helpful response.
>
> To everyone else... I know that Nikos' posts are draining. Sometimes he
> brings me to the brink of despair too. But if you aren't part of the
> solution, you are part of the problem: writing short-tempered, insulting
> posts after short-tempered, insulting post doesn't teach him, it just
> adds to EVERYBODY'S frustration with this never-ending serious of threads.

Notice your spelling of series Steven?

From which I suggest you seriously(!) consider my earlier suggestion
that Nikos is an infectious disease.

[toc] | [prev] | [next] | [standalone]


#48200

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2013-06-14 20:03 +0200
Message-ID<mailman.3331.1371233064.3114.python-list@python.org>
In reply to#48170
Op 14-06-13 18:09, Steven D'Aprano schreef:
> On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:
>
>> On 2013-06-14 10:50, Nick the Gr33k wrote:
> [snip question]
>>> This is all iw ant to know.
>>
>> This is all you need to read:
>>
>>     http://docs.python.org/2/reference/expressions.html#boolean-
> operations
>
>
> Thank you Robert for contributing a helpful response.
>
> To everyone else... I know that Nikos' posts are draining. Sometimes he
> brings me to the brink of despair too. But if you aren't part of the
> solution, you are part of the problem: writing short-tempered, insulting
> posts after short-tempered, insulting post doesn't teach him, it just
> adds to EVERYBODY'S frustration with this never-ending serious of threads.

That depends on your view. Maybe there are people who view you as part 
of the problem because you keep giving Nikos hope other people will
solve his problems without him having to do anything significant himself.

> Please keep the snarky comments offlist. Contribute something helpful if
> you like, mute the thread or killfile Nikos if you must, but adding to
> the volume of unproductive junk doesn't do anyone any favour.

In my opinion the only really helpful thing would be to ignore nikos. He
has been given plenty or links, to read through in order to better his
understanding and i think it is about time people made it clear they
would no longer spoon feed him. Since that is probably not going to
happen and you will probably continue contributing to the annoyance
of other list members by continuing "helping" nikos, I don'y see why
other can't continue to the annoyance in a away that they themselves may 
find somewhat amusing.

-- 
Antoon Pardon.

[toc] | [prev] | [next] | [standalone]


#48203

Fromrusi <rustompmody@gmail.com>
Date2013-06-14 11:37 -0700
Message-ID<68537a4d-3b48-4220-a67b-64015301ccdf@q10g2000pbc.googlegroups.com>
In reply to#48200
On Jun 14, 11:03 pm, Antoon Pardon <antoon.par...@rece.vub.ac.be>
wrote:
> Op 14-06-13 18:09, Steven D'Aprano schreef:
>
>
>
>
>
>
>
>
>
> > On Fri, 14 Jun 2013 11:14:16 +0100, Robert Kern wrote:
>
> >> On 2013-06-14 10:50, Nick the Gr33k wrote:
> > [snip question]
> >>> This is all iw ant to know.
>
> >> This is all you need to read:
>
> >>    http://docs.python.org/2/reference/expressions.html#boolean-
> > operations
>
> > Thank you Robert for contributing a helpful response.
>
> > To everyone else... I know that Nikos' posts are draining. Sometimes he
> > brings me to the brink of despair too. But if you aren't part of the
> > solution, you are part of the problem: writing short-tempered, insulting
> > posts after short-tempered, insulting post doesn't teach him, it just
> > adds to EVERYBODY'S frustration with this never-ending serious of threads.
>
> That depends on your view. Maybe there are people who view you as part
> of the problem because you keep giving Nikos hope other people will
> solve his problems without him having to do anything significant himself.
>
> > Please keep the snarky comments offlist. Contribute something helpful if
> > you like, mute the thread or killfile Nikos if you must, but adding to
> > the volume of unproductive junk doesn't do anyone any favour.
>
> In my opinion the only really helpful thing would be to ignore nikos. He
> has been given plenty or links, to read through in order to better his
> understanding and i think it is about time people made it clear they
> would no longer spoon feed him. Since that is probably not going to
> happen and you will probably continue contributing to the annoyance
> of other list members by continuing "helping" nikos, I don'y see why
> other can't continue to the annoyance in a away that they themselves may
> find somewhat amusing.
>
> --
> Antoon Pardon.

Thanks Antoon.
You are echoing the sentiments of probably hundreds of list
subscribers.

That said I would like to add a few points.
1. Beginning teachers (and young parents) are taught that one never
should say: "You are a bad boy!" but rather "THIS behavior wont do"
That nikos behavior is unacceptable has been reiterated by dozens of
members in 100s of posts.  That does not mean Nikos the person is
unacceptable.  We should always give him the room to change if/when he
chooses to.

2. The recent responses from Robert Kern are in my view the ideal. In
summary it runs thus:
Stupid question no. 6457 from Nikos: ...
Robert : Look this up <link>
Nikos: I dont understand
Robert: <Link> explains
Nikos: I DONTU NDERSTND
Robert: <Link> explains (repeated as often as needed)

When (if) finally Nikos actually reads the link and asks a more
specific/intelligent question, <link> can be replaced by more specific
<link'>

[toc] | [prev] | [next] | [standalone]


#48205

FromJoshua Landau <joshua.landau.ws@gmail.com>
Date2013-06-14 19:47 +0100
Message-ID<mailman.3334.1371235683.3114.python-list@python.org>
In reply to#48203
On 14 June 2013 19:37, rusi <rustompmody@gmail.com> wrote:
> 2. The recent responses from Robert Kern are in my view the ideal. In
> summary it runs thus:
> Stupid question no. 6457 from Nikos: ...
> Robert : Look this up <link>
> Nikos: I dont understand
> Robert: <Link> explains
> Nikos: I DONTU NDERSTND
> Robert: <Link> explains (repeated as often as needed)
>
> When (if) finally Nikos actually reads the link and asks a more
> specific/intelligent question, <link> can be replaced by more specific
> <link'>

+1

Please, everyone else, just do this. This will calm the beast, I
assure you, at least somewhat. If you're worried about not being
"helpful", don't - this is as much help as ever. It's just it takes
less time, and a *lot* less space.

[toc] | [prev] | [next] | [standalone]


#48259

Fromalex23 <wuwei23@gmail.com>
Date2013-06-14 23:50 -0700
Message-ID<3a39c69a-5a37-4441-8e34-cd58044c8d05@googlegroups.com>
In reply to#48170
On Saturday, 15 June 2013 02:09:20 UTC+10, Steven D'Aprano  wrote:
> To everyone else... I know that Nikos' posts are draining. Sometimes he 
> brings me to the brink of despair too. But if you aren't part of the 
> solution, you are part of the problem: writing short-tempered, insulting 
> posts after short-tempered, insulting post doesn't teach him, it just 
> adds to EVERYBODY'S frustration with this never-ending serious of threads.

What about our frustration that this thread has become so overwhelmingly his support group? It's not like there isn't a list where he could post absolute beginner questions:

http://mail.python.org/mailman/listinfo/tutor

Although I'm pretty sure their first response would be "read the docs / play with the interpreter", neither of which he's shown any desire to do.

> Please keep the snarky comments offlist. 

Tried that. He posts them back here.

Alternatively, I'd ask that if you're so willing to deal with him, that the *two of you* take this show offlist instead? I'm genuinely curious as to whether he'd agree to this: given his propensity for changing his mail headers so regularly, the whole thing still screams performance troll. (I mean, seriously, how many days elapsed between his being burned by Chris Angelo and him offering to give you access to his server? Isn't refusing to budge from a position the defining characteristic of a ferrous cranus?)

[toc] | [prev] | [next] | [standalone]


#48261

FromNick the Gr33k <support@superhost.gr>
Date2013-06-15 10:04 +0300
Message-ID<kph3m9$29li$2@news.ntua.gr>
In reply to#48259
On 15/6/2013 9:50 πμ, alex23 wrote:
>> Please keep the snarky comments offlist.
> Tried that. He posts them back here.
>
> Alternatively, I'd ask that if you're so willing to deal with him, that the *two of you* take this show offlist instead? I'm genuinely curious as to whether he'd agree to this: given his propensity for changing his mail headers so regularly, the whole thing still screams performance troll. (I mean, seriously, how many days elapsed between his being burned by Chris Angelo and him offering to give you access to his server? Isn't refusing to budge from a position the defining characteristic of a ferrous cranus?)

I called my self 'Ferrous Cranus'(this is what a guy from a forum 
initially called me for being hard-headed :-) ) because i'm indeed 
hardheaded and if i believe that 1 thing should have worked in some way 
i cant change my mind easily that it works in another fashon(only if the 
counter-arguments are strong).
Being strong-headed != acting as a troll


About the changing of NNTP hosts. (well Google Groups gave me and you 
issues with its constant adding of '\n' between lines. I was given many 
complains about that so as Chris suggested i ditched Google Groups and 
try to find out how i can use TB instead.

As for the mail, i decided since my questions concern python and my 
website to change the gmail address to support@.... which is more relevant.

I have also correct each time my spelling before i post.

Now as for you, you should be thankful for Steven and Cameron answers to 
this groups because answering my questions.

1. help me
2. help others, which afraid to ask, in the propensity of being laughed upon
3. help other groups members like Michale Torrie whose admitted that 
some of the explanation was useful to him and he didn't knew that 
Boolean expressions can return string values instead of True or False.
3. help you, because you are being educated yourself too, because i 
sincerely doubt if you knew anything i i have asked.

So, stop complaining and try to be helpful here or just  mute my thread 
as Chris suggested as a working solution opposed to kill filing.


-- 
What is now proved was at first only imagined!

[toc] | [prev] | [next] | [standalone]


#48265

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-06-15 07:49 +0000
Message-ID<kph6ai$su9$7@dont-email.me>
In reply to#48261
On Sat, 15 Jun 2013 10:04:41 +0300, Nick the Gr33k wrote:

> I called my self 'Ferrous Cranus'(this is what a guy from a forum
> initially called me for being hard-headed :-) ) because i'm indeed
> hardheaded and if i believe that 1 thing should have worked in some way
> i cant change my mind easily that it works in another fashon(only if the
> counter-arguments are strong).

Then you need to stop trying to write python code, because you refuse to 
accept how python works, and python is not going to change for you!

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#48267

FromNick the Gr33k <support@superhost.gr>
Date2013-06-15 10:59 +0300
Message-ID<kph6s7$29li$4@news.ntua.gr>
In reply to#48265
On 15/6/2013 10:49 πμ, Denis McMahon wrote:
> On Sat, 15 Jun 2013 10:04:41 +0300, Nick the Gr33k wrote:
>
>> I called my self 'Ferrous Cranus'(this is what a guy from a forum
>> initially called me for being hard-headed :-) ) because i'm indeed
>> hardheaded and if i believe that 1 thing should have worked in some way
>> i cant change my mind easily that it works in another fashon(only if the
>> counter-arguments are strong).
>
> Then you need to stop trying to write python code, because you refuse to
> accept how python works, and python is not going to change for you!

Given the right counter-arguments one can't make a stand against 
something that is without no doubt True.


-- 
What is now proved was at first only imagined!

[toc] | [prev] | [next] | [standalone]


#48158

FromGrant Edwards <invalid@invalid.invalid>
Date2013-06-14 14:49 +0000
Message-ID<kpfai4$o0e$4@reader1.panix.com>
In reply to#48106
On 2013-06-14, Nick the Gr33k <support@superhost.gr> wrote:

> I started another thread

no kidding.

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

Yes, it does.  That's the way the language is defined to work.  If you
don't like it, pick a different language.

> >>> print("k" in (name and month and year))
> True
>
> No clue. since the expression in parenthesis returns 'abcd'

No it doesn't.  Try it:

Python 2.7.3 (default, Mar 20 2013, 14:16:24) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> name="abcd"
>>> month="efgh"
>>> year="ijkl"
>>> 
>>> "k" in (name and month and year)
True
>>> (name and month and year)
'ijkl'
>>> 

> how can 'k' contained within 'abcd' ?

It doesn't

-- 
Grant Edwards               grant.b.edwards        Yow! Am I having fun yet?
                                  at               
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#48161

FromNick the Gr33k <support@superhost.gr>
Date2013-06-14 18:16 +0300
Message-ID<kpfc3k$1p7m$1@news.ntua.gr>
In reply to#48158
On 14/6/2013 5:49 μμ, Grant Edwards wrote:
> On 2013-06-14, Nick the Gr33k <support@superhost.gr> wrote:
>
>> I started another thread
>
> no kidding.
>
>> 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.
>
> Yes, it does.  That's the way the language is defined to work.  If you
> don't like it, pick a different language.

I can understand OR stops at the first value when it finds it truthy, 
and doesn't care for the other expr parameters.

And that logical since we say:

(name or month or year)
is like saying:

print whatever of those 3 are True, at least 1 of them, if it doesn't 
not find any though for some reason it defaults to the last variable of 
the expression(another mystery for me)

the way i realize it is just that for this boolean evaluation of the 
expression to result in True if any of the above vars holds a truthy value.


Nice try, moving me to another newsgroup list :)

>>>>> print("k" in (name and month and year))
>> True
>>
>> No clue. since the expression in parenthesis returns 'abcd'
>
> No it doesn't.  Try it:
>
> Python 2.7.3 (default, Mar 20 2013, 14:16:24)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>> name="abcd"
>>>> month="efgh"
>>>> year="ijkl"
>>>>
>>>> "k" in (name and month and year)
> True
>>>> (name and month and year)
> 'ijkl'
>>>>
>
>> how can 'k' contained within 'abcd' ?
Um sorry was a  typo.

I mean the expr eval results in the value of var year which is 'ijkl'.

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

So yes it does.

My question is why the expr (name and month and year) result in the 
value of the last variable whic is variable year?

I cannot understand AND.

Isn't it like saying that:
(name and month and year)

all of these variables have to have truthy values so for the boolaean 
eval of the expr result in True?

-- 
What is now proved was at first only imagined!

[toc] | [prev] | [next] | [standalone]


#48178

FromNobody <nobody@nowhere.com>
Date2013-06-14 17:42 +0100
Message-ID<pan.2013.06.14.16.42.08.620000@nowhere.com>
In reply to#48161
On Fri, 14 Jun 2013 18:16:05 +0300, Nick the Gr33k wrote:

> My question is why the expr (name and month and year) result in the 
> value of the last variable whic is variable year?

For much the same reason that an OR expression returns the first true
value.

"or" and "and" only evaluate as many arguments are required in order to
determine the correct result (aka "short-circuit evaluation"). If the
first argument of "or" is true, or the first argument of "and" is false,
the second argument isn't evaluated (this is important if evaluation can
have side effects).

The operators can be expressed as:

	True or X = True
	False or X = X

	False and X = False
	True and X = X

Note that in the short-circuit case, the result has the same sense (true
or false) as the first argument, while in the other case the result has
the same sense as the second argument.

Python implements these operators by returning the actual value which
determined the result of the expression rather than simply True or False.

If the result is known after evaluating the first argument, the first
argument is returned. If it has to evaluate the second argument, the
second argument is returned (by that point it has already forgotten
the value of the first argument).

[toc] | [prev] | [next] | [standalone]


#48213

FromGrant Edwards <invalid@invalid.invalid>
Date2013-06-14 19:30 +0000
Message-ID<kpfr0j$sd2$1@reader1.panix.com>
In reply to#48178
On 2013-06-14, Nobody <nobody@nowhere.com> wrote:
> On Fri, 14 Jun 2013 18:16:05 +0300, Nick the Gr33k wrote:
>
>> My question is why the expr (name and month and year) result in the 
>> value of the last variable whic is variable year?
>
> For much the same reason that an OR expression returns the first true
> value.
>
> "or" and "and" only evaluate as many arguments are required in order to
> determine the correct result (aka "short-circuit evaluation"). If the
> first argument of "or" is true, or the first argument of "and" is false,
> the second argument isn't evaluated (this is important if evaluation can
> have side effects).

There are two completely orthogonal concepts here:

 1. Short-circuit evaluation.  Many languages do this.  AFAICT, this
    isn't what he's asking about, but this is what people keep
    explaining.

 2. Returning one the objects that result from the evaluation of the
    operands instead of returning True or False.

    This is what seems to be confusing him.  This is much less common
    than short-circuit evaluation.  C does short-circuit evaluation of
    && and || operators, but the result is always 1 or 0 (true of
    false). Instead of always returning True or False (which could be
    done and still preserver short-circuit evaluation), Python returns
    one of the operands or False.

    If you also have 1. there are cases where the value returned by
    the "or" operator is useful apart from it's "truthyness" value.
    There may be cases where the result returned by the "and" operator
    is useful apart from it's truthyness, but that seems to be less
    common.  Taking advantage of that fact can lead to some
    hard-to-read code, so it's often discouraged as being too clever.

It's important to note that these are somewhat orthogonal:

 You can have #1 and #2 (like Python).

 You can have #1 without #2 (like C).

 You can have #2 without #1

 You can have neither #1 or #2

But again, #2 is more useful if you also have #1. 
 
-- 
Grant Edwards               grant.b.edwards        Yow! ... I have read the
                                  at               INSTRUCTIONS ...
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#48221

FromNobody <nobody@nowhere.com>
Date2013-06-15 00:02 +0100
Message-ID<pan.2013.06.14.23.02.56.121000@nowhere.com>
In reply to#48213
On Fri, 14 Jun 2013 19:30:27 +0000, Grant Edwards wrote:

>  2. Returning one the objects that result from the evaluation of the
>     operands instead of returning True or False.
> 
>     This is what seems to be confusing him.  This is much less common
>     than short-circuit evaluation.

FWIW, Lisp also does this. But Lisp is slightly simpler as the only false
value is "nil", while everything else is true (including integer zero).

Although Python's any() and all() (which are closer to Lisp's "and" and
"or" insofar as they all work with any number of values, including zero)
always return True or False rather than the final value.

[toc] | [prev] | [next] | [standalone]


#48266

FromNick the Gr33k <support@superhost.gr>
Date2013-06-15 10:57 +0300
Message-ID<kph6oh$29li$3@news.ntua.gr>
In reply to#48178
On 14/6/2013 7:42 μμ, Nobody wrote:
> Python implements these operators by returning the actual value which
> determined the result of the expression rather than simply True or False.

which in turn the actual value being returned is a truthy or a falsey.

That cleared the mystery in my head entirely.
I wouldn't have asked so many follow-up questions in the thread if i 
received that kind of a response.

Thank you very much for this response.

> If the result is known after evaluating the first argument, the first
> argument is returned. If it has to evaluate the second argument, the
> second argument is returned (by that point it has already forgotten
> the value of the first argument).

So, the less it has to calculate to determine the correct result of an 
expression the better.

Thanks again very much.

-- 
What is now proved was at first only imagined!

[toc] | [prev] | [next] | [standalone]


#48176

FromMichael Torrie <torriem@gmail.com>
Date2013-06-14 10:29 -0600
Message-ID<mailman.3314.1371227385.3114.python-list@python.org>
In reply to#48106
On 06/14/2013 03:50 AM, Nick the Gr33k wrote:
>  >>> print(name or month or year)
> abcd
>  >>> print(name and month and year)
> ijkl

Interesting.  I'd have thought a boolean expression would return True or
False, not a string.  Learn something new every day.


[toc] | [prev] | [next] | [standalone]


#48181

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-14 16:49 +0000
Message-ID<51bb4986$0$29997$c3e8da3$5496439d@news.astraweb.com>
In reply to#48176
On Fri, 14 Jun 2013 10:29:25 -0600, Michael Torrie wrote:

> On 06/14/2013 03:50 AM, Nick the Gr33k wrote:
>>  >>> print(name or month or year)
>> abcd
>>  >>> print(name and month and year)
>> ijkl
> 
> Interesting.  I'd have thought a boolean expression would return True or
> False, not a string.  Learn something new every day.

Correct. In Python, all boolean expressions are duck-typed: they aren't 
restricted to True and False, but to any "true-ish" and "false-ish" 
value, or as the Javascript people call them, truthy and falsey values.

Unlike Javascript though, Python's idea of truthy and falsey is actually 
quite consistent:


Truthy:
- True
- any non-zero number
- any non-empty string
- any non-empty list/tuple/dict/set etc.
- object()
- anything else best considered as "something"


Falsey:
- False
- zero
- empty string
- empty list/tuple/dict/set etc.
- None
- anything else best considered as "nothing"


There are a couple of anomalies -- the timestamp representing midnight is 
falsey, because it is implemented as a zero number of seconds; also 
exhausted iterators and generators ought to be considered falsey, since 
they are empty, but because they don't know they are empty until called, 
they are actually treated as truthy. But otherwise, the model is very 
clean.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.python


csiph-web