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


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

Re: A certainl part of an if() structure never gets executed.

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2013-06-13 01:55 +0000
Last post2013-06-14 10:05 +0100
Articles 20 on this page of 115 — 33 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: A certainl part of an if() structure never gets executed. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-13 01:55 +0000
    Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-13 12:03 +1000
    Re: A certainl part of an if() structure never gets executed. Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2013-06-13 10:05 +0530
    Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-13 14:39 +1000
    Re: A certainl part of an if() structure never gets executed. Νικόλαος Κούρας <support@superhost.gr> - 2013-06-13 08:36 +0300
    Re: A certainl part of an if() structure never gets executed. Νικόλαος Κούρας <support@superhost.gr> - 2013-06-13 10:11 +0300
      Re: A certainl part of an if() structure never gets executed. Sibylle Koczian <nulla.epistola@web.de> - 2013-06-13 14:22 +0200
        Re: A certainl part of an if() structure never gets executed. Νικόλαος Κούρας <support@superhost.gr> - 2013-06-13 17:26 +0300
          Re: A certainl part of an if() structure never gets executed. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-14 01:14 +0000
            Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 11:03 +0300
              Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-14 18:23 +1000
              Re: A certainl part of an if() structure never gets executed. "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2013-06-14 09:24 +0100
              Re: A certainl part of an if() structure never gets executed. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-14 11:28 +0300
                Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 11:41 +0300
                  Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-14 18:50 +1000
                  Re: A certainl part of an if() structure never gets executed. Fábio Santos <fabiosantosart@gmail.com> - 2013-06-14 10:03 +0100
                  Re: A certainl part of an if() structure never gets executed. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-14 12:21 +0300
                    Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 12:44 +0300
                      Re: A certainl part of an if() structure never gets executed. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-14 15:40 +0300
                        Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 16:07 +0300
                          Re: A certainl part of an if() structure never gets executed. Zero Piraeus <schesis@gmail.com> - 2013-06-14 09:48 -0400
                            Re: A certainl part of an if() structure never gets executed. rusi <rustompmody@gmail.com> - 2013-06-14 07:05 -0700
                            Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 17:08 +0300
                          Re: A certainl part of an if() structure never gets executed. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-14 16:31 +0000
                            Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 19:56 +0300
                              Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-15 03:18 +1000
                              Re: A certainl part of an if() structure never gets executed. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-14 21:17 +0300
                              Re: A certainl part of an if() structure never gets executed. Larry Hudson <orgnut@yahoo.com> - 2013-06-14 22:27 -0700
                                Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-15 11:39 +0300
                                  Re: A certainl part of an if() structure never gets executed. Lele Gaifax <lele@metapensiero.it> - 2013-06-15 11:54 +0200
                                    Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-15 16:07 +0300
                                      Re: A certainl part of an if() structure never gets executed. Michael Torrie <torriem@gmail.com> - 2013-06-15 09:53 -0600
                                        Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-15 19:18 +0300
                                          Re: A certainl part of an if() structure never gets executed. Michael Torrie <torriem@gmail.com> - 2013-06-15 11:45 -0600
                                          Re: A certainl part of an if() structure never gets executed. Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-16 06:32 +0000
                                            Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-16 11:07 +0300
                                              Re: A certainl part of an if() structure never gets executed. Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-16 09:22 +0000
                                                Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-16 12:59 +0300
                                                  Re: A certainl part of an if() structure never gets executed. "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2013-06-16 11:42 +0100
                                                    Re: A certainl part of an if() structure never gets executed. Ferrous Cranus <support@superhost.gr> - 2013-06-16 14:06 +0300
                                                      Re: A certainl part of an if() structure never gets executed. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-16 12:26 +0100
                                                      Re: A certainl part of an if() structure never gets executed. YBM <ybmess@nooos.fr.invalid> - 2013-06-16 14:00 +0200
                                                      Re: A certainl part of an if() structure never gets executed. "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2013-06-16 13:04 +0100
                                                        Re: A certainl part of an if() structure never gets executed. Ferrous Cranus <support@superhost.gr> - 2013-06-16 16:38 +0300
                                                          Re: A certainl part of an if() structure never gets executed. "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2013-06-16 19:50 +0100
                                                  Re: A certainl part of an if() structure never gets executed. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-16 11:52 +0100
                                                  Re: A certainl part of an if() structure never gets executed. Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-16 10:51 +0000
                                                    Compiling vs interpreting [was Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-16 12:07 +0000
                                                      Re: Compiling vs interpreting [was Re: A certainl part of an if() structure never gets executed.] Mark Janssen <dreamingforward@gmail.com> - 2013-06-16 12:31 -0700
                                                        Re: Compiling vs interpreting [was Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-16 20:02 +0000
                                                          Re: Compiling vs interpreting [was Re: A certainl part of an if() structure never gets executed.] Chris Angelico <rosuav@gmail.com> - 2013-06-17 08:26 +1000
                                                      Re: Compiling vs interpreting [was Re: A certainl part of an if() structure never gets executed.] Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-16 23:13 -0400
                                                  Re: A certainl part of an if() structure never gets executed. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-16 14:13 +0300
                                                    Re: A certainl part of an if() structure never gets executed. Ferrous Cranus <support@superhost.gr> - 2013-06-16 16:47 +0300
                                                      Re: A certainl part of an if() structure never gets executed. "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2013-06-16 19:53 +0100
                                                        Re: A certainl part of an if() structure never gets executed. Νίκος <support@superhost.gr> - 2013-06-17 08:17 +0300
                                                          Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-17 06:51 +0000
                                                            Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Simpleton <support@superhost.gr> - 2013-06-17 14:34 +0300
                                                              Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Michael Torrie <torriem@gmail.com> - 2013-06-17 05:58 -0600
                                                                Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Simpleton <support@superhost.gr> - 2013-06-17 18:50 +0300
                                                                  Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Larry Hudson <orgnut@yahoo.com> - 2013-06-17 23:39 -0700
                                                                    Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-18 07:24 +0000
                                                                    Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Νίκος <support@superhost.gr> - 2013-06-18 11:49 +0300
                                                                      Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-18 09:05 +0000
                                                                        Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Νίκος <support@superhost.gr> - 2013-06-18 12:51 +0300
                                                                          Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Chris Angelico <rosuav@gmail.com> - 2013-06-18 20:22 +1000
                                                                          Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Michael Torrie <torriem@gmail.com> - 2013-06-19 23:16 -0600
                                                                            Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-20 05:48 +0000
                                                                              Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Michael Torrie <torriem@gmail.com> - 2013-06-20 00:01 -0600
                                                                                Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] 88888 Dihedral <dihedral88888@gmail.com> - 2013-06-26 01:18 -0700
                                                                          Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Michael Torrie <torriem@gmail.com> - 2013-06-19 23:44 -0600
                                                                          Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Roel Schroeven <roel@roelschroeven.net> - 2013-06-20 19:19 +0200
                                                              Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Terry Reedy <tjreedy@udel.edu> - 2013-06-17 10:22 -0400
                                                                Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Simpleton <support@superhost.gr> - 2013-06-17 18:55 +0300
                                                                  Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Joel Goldstick <joel.goldstick@gmail.com> - 2013-06-17 12:26 -0400
                                                                  Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Benjamin Kaplan <benjamin.kaplan@case.edu> - 2013-06-17 09:23 -0700
                                                                    Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Νίκος <support@superhost.gr> - 2013-06-17 20:17 +0300
                                                                      Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Terry Reedy <tjreedy@udel.edu> - 2013-06-17 18:16 -0400
                                                              Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-17 23:09 +0000
                                                                Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Νίκος <support@superhost.gr> - 2013-06-18 02:26 +0300
                                                                  Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-18 00:41 +0000
                                                                    Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Dave Angel <davea@davea.name> - 2013-06-17 21:06 -0400
                                                                      Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-18 02:42 +0000
                                                                        Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Dave Angel <davea@davea.name> - 2013-06-18 00:12 -0400
                                                                          Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-18 06:04 +0000
                                                                    Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-18 02:38 +0000
                                                                      Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-18 02:46 +0000
                                                                  Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-17 21:34 -0400
                                                                  Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Marcin Szamotulski <mszamot@gmail.com> - 2013-06-18 04:22 +0100
                                                          Re: A certainl part of an if() structure never gets executed. Michael Weylandt <michael.weylandt@gmail.com> - 2013-06-17 07:56 +0100
                                                Re: A certainl part of an if() structure never gets executed. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-16 12:50 +0000
                                            OT: C vs Python terminology (was: A certainl part of an if() structure never gets executed) Andreas Perstinger <andipersti@gmail.com> - 2013-06-16 13:22 +0200
                                            Re: OT: C vs Python terminology Dave Angel <davea@davea.name> - 2013-06-16 08:55 -0400
                                            Re: OT: C vs Python terminology Andreas Perstinger <andipersti@gmail.com> - 2013-06-16 17:02 +0200
                                            Re: OT: C vs Python terminology Dave Angel <davea@davea.name> - 2013-06-16 21:58 -0400
              Re: A certainl part of an if() structure never gets executed. "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2013-06-14 09:28 +0100
              Re: A certainl part of an if() structure never gets executed. Fábio Santos <fabiosantosart@gmail.com> - 2013-06-14 09:35 +0100
              Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 11:44 +0300
                Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-14 18:57 +1000
                  Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 12:00 +0300
                    Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-14 19:12 +1000
                      Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-14 12:47 +0300
                        Re: A certainl part of an if() structure never gets executed. Tim Roberts <timr@probo.com> - 2013-06-15 18:55 -0700
                          Re: A certainl part of an if() structure never gets executed. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-16 05:09 +0000
                          Re: A certainl part of an if() structure never gets executed. Nick the Gr33k <support@superhost.gr> - 2013-06-16 11:20 +0300
                            Re: A certainl part of an if() structure never gets executed. Tim Roberts <timr@probo.com> - 2013-06-18 22:08 -0700
                              Re: A certainl part of an if() structure never gets executed. Dave Angel <davea@davea.name> - 2013-06-19 01:42 -0400
                              Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-19 17:14 +1000
                              Re: A certainl part of an if() structure never gets executed. Νίκος <support@superhost.gr> - 2013-06-19 10:49 +0300
                              Re: A certainl part of an if() structure never gets executed. Dave Angel <davea@davea.name> - 2013-06-19 04:06 -0400
                              Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-19 18:21 +1000
                                Re: A certainl part of an if() structure never gets executed. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-19 08:55 +0000
                                  Re: A certainl part of an if() structure never gets executed. Chris Angelico <rosuav@gmail.com> - 2013-06-19 19:14 +1000
                    Re: A certainl part of an if() structure never gets executed. Grant Edwards <invalid@invalid.invalid> - 2013-06-14 14:38 +0000
                Re: A certainl part of an if() structure never gets executed. Fábio Santos <fabiosantosart@gmail.com> - 2013-06-14 10:05 +0100

Page 1 of 6  [1] 2 3 4 5 6  Next page →


#47884 — Re: A certainl part of an if() structure never gets executed.

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-13 01:55 +0000
SubjectRe: A certainl part of an if() structure never gets executed.
Message-ID<51b926a3$0$29997$c3e8da3$5496439d@news.astraweb.com>
On Wed, 12 Jun 2013 14:17:32 +0300, Νικόλαος Κούρας wrote:

> doesn't that mean?
> 
> 		if '=' not in ( name and month and year ):
> 
> if '=' does not exists as a char inside the name and month and year
> variables?
> 
> i think it does, but why it fails then?

No. Python is very close to "English-like", but not exactly, and this is 
one of the easiest places to trip.

In English:

"the cat is in the box or the cupboard or the kitchen"

means:

"the cat is in the box, or the cat is in the cupboard, or the cat is in 
the kitchen".


But that is not how Python works. In Python, you have to say:

cat in box or cat in cupboard or cat in kitchen


Although this will work as well:

any(cat in place for place in (box, cupboard, kitchen)) 


In Python, an expression like this:

cat in (box or cupboard or kitchen)


has a completely different meaning. First, the expression in the round 
brackets is evaluated:

(box or cupboard or kitchen)


and then the test is performed:

cat in (result of the above)


The expression (box or cupboard or kitchen) means "return the first one 
of box, cupboard, kitchen that is a truthy value, otherwise the last 
value". Truthy values are those which are considered to be "like True":

truthy values:

- True
- object()
- numbers apart from zero
- non-empty strings
- non-empty lists
- non-empty sets
- non-empty dicts
- etc.

falsey:

- False
- None
- zero (0, 0.0, Decimal(0), Fraction(0), etc.)
- empty string
- empty list
- empty set
- empty dict
- etc.

(Can you see the pattern?)


So you can experiment with this yourself:

42 or 23 or "foo"
=> the first object is truthy, so it is returned

0 or 23 or "foo"
=> the first object is falsey, and the second object is truthy, 
so it is returned

0 or [] or "foo"
=> the first two objects are falsey, so the third is returned


The "and" operator works in a similar fashion. Experiment with it and see 
how it works for yourself.



-- 
Steven

[toc] | [next] | [standalone]


#47885

FromChris Angelico <rosuav@gmail.com>
Date2013-06-13 12:03 +1000
Message-ID<mailman.3154.1371089041.3114.python-list@python.org>
In reply to#47884
On Thu, Jun 13, 2013 at 11:55 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> In English:
>
> "the cat is in the box or the cupboard or the kitchen"
>
> means:
>
> "the cat is in the box, or the cat is in the cupboard, or the cat is in
> the kitchen".
>
>
> But that is not how Python works. In Python, you have to say:
>
> cat in box or cat in cupboard or cat in kitchen

Or you can deem that there be one single location that is the merging
of box, cupboard, and kitchen, and say:

cat in (box+cupboard+kitchen)

which works fine for character-in-string and element-in-list searches.

ChrisA

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


#47894

FromKushal Kumaran <kushal.kumaran+python@gmail.com>
Date2013-06-13 10:05 +0530
Message-ID<mailman.3160.1371098165.3114.python-list@python.org>
In reply to#47884
Chris Angelico <rosuav@gmail.com> writes:

> On Thu, Jun 13, 2013 at 11:55 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> In English:
>>
>> "the cat is in the box or the cupboard or the kitchen"
>>
>> means:
>>
>> "the cat is in the box, or the cat is in the cupboard, or the cat is in
>> the kitchen".
>>
>>
>> But that is not how Python works. In Python, you have to say:
>>
>> cat in box or cat in cupboard or cat in kitchen
>
> Or you can deem that there be one single location that is the merging
> of box, cupboard, and kitchen, and say:
>
> cat in (box+cupboard+kitchen)
>
> which works fine for character-in-string and element-in-list searches.
>

hm...

In [1]: s1 = 'abc'

In [2]: s2 = 'def'

In [3]: s3 = 'ghi'

In [4]: 'cd' in s1 or 'cd' in s2 or 'cd' in s3
Out[4]: False

In [5]: 'cd' in s1+s2+s3
Out[5]: True

-- 
regards,
kushal

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


#47895

FromChris Angelico <rosuav@gmail.com>
Date2013-06-13 14:39 +1000
Message-ID<mailman.3161.1371098350.3114.python-list@python.org>
In reply to#47884
On Thu, Jun 13, 2013 at 2:35 PM, Kushal Kumaran
<kushal.kumaran+python@gmail.com> wrote:
> In [4]: 'cd' in s1 or 'cd' in s2 or 'cd' in s3
> Out[4]: False
>
> In [5]: 'cd' in s1+s2+s3
> Out[5]: True

That's why I said it works for *character* in string, not *string* in
string. If your first operand is a single character (which in Python
is still of type 'str'), then the equivalency holds.

ChrisA

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


#47899

FromΝικόλαος Κούρας <support@superhost.gr>
Date2013-06-13 08:36 +0300
Message-ID<kpblpc$qvk$1@news.ntua.gr>
In reply to#47884
On 13/6/2013 4:55 πμ, Steven D'Aprano wrote:
> On Wed, 12 Jun 2013 14:17:32 +0300, Νικόλαος Κούρας wrote:
>
>> doesn't that mean?
>>
>> 		if '=' not in ( name and month and year ):
>>
>> if '=' does not exists as a char inside the name and month and year
>> variables?
>>
>> i think it does, but why it fails then?
>
> No. Python is very close to "English-like", but not exactly, and this is
> one of the easiest places to trip.
>
> In English:
>
> "the cat is in the box or the cupboard or the kitchen"
>
> means:
>
> "the cat is in the box, or the cat is in the cupboard, or the cat is in
> the kitchen".
>
>
> But that is not how Python works. In Python, you have to say:
>
> cat in box or cat in cupboard or cat in kitchen
>
>
> Although this will work as well:
>
> any(cat in place for place in (box, cupboard, kitchen))
>
>
> In Python, an expression like this:
>
> cat in (box or cupboard or kitchen)
>
>
> has a completely different meaning. First, the expression in the round
> brackets is evaluated:
>
> (box or cupboard or kitchen)
>
>
> and then the test is performed:
>
> cat in (result of the above)
>
>
> The expression (box or cupboard or kitchen) means "return the first one
> of box, cupboard, kitchen that is a truthy value, otherwise the last
> value". Truthy values are those which are considered to be "like True":
>
> truthy values:
>
> - True
> - object()
> - numbers apart from zero
> - non-empty strings
> - non-empty lists
> - non-empty sets
> - non-empty dicts
> - etc.
>
> falsey:
>
> - False
> - None
> - zero (0, 0.0, Decimal(0), Fraction(0), etc.)
> - empty string
> - empty list
> - empty set
> - empty dict
> - etc.
>
> (Can you see the pattern?)
>
>
> So you can experiment with this yourself:
>
> 42 or 23 or "foo"
> => the first object is truthy, so it is returned
>
> 0 or 23 or "foo"
> => the first object is falsey, and the second object is truthy,
> so it is returned
>
> 0 or [] or "foo"
> => the first two objects are falsey, so the third is returned
>
>
> The "and" operator works in a similar fashion. Experiment with it and see
> how it works for yourself.

First i wan tot thank you for taking the time to explain to me (the 
languages examples to explai encode-decode was really great as well)


So, baring in ming your explanation:

if '=' not in ( name or month or year )

first eval the expr. The expr will result to return the first object 
that has a truthy values.

in this particular example all 3 strings are truthies because they all 
contain characters inside them, the one user submitted when hitting the 
submit button

so that would turn to if '=' not in ( name ).
Allright, but what i wanted to check is if the char '=' ain't contained 
in both 3 strings not for the 1st string(name) which will always be true.

So i guess having it like this "if '=' not in ( name or month or year )" 
  is wrong?


b) Also what "if '=' not in ( name and month and year )" returns in the 
round brackets since in my case all the strings have values therefore 
they are true, which string get returned?

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


#47913

FromΝικόλαος Κούρας <support@superhost.gr>
Date2013-06-13 10:11 +0300
Message-ID<kpbraq$qvk$6@news.ntua.gr>
In reply to#47884
On 13/6/2013 4:55 πμ, Steven D'Aprano wrote:
> On Wed, 12 Jun 2013 14:17:32 +0300, Νικόλαος Κούρας wrote:
>
>> doesn't that mean?
>>
>> 		if '=' not in ( name and month and year ):
>>
>> if '=' does not exists as a char inside the name and month and year
>> variables?
>>
>> i think it does, but why it fails then?
>
> No. Python is very close to "English-like", but not exactly, and this is
> one of the easiest places to trip.
>
> In English:
>
> "the cat is in the box or the cupboard or the kitchen"
>
> means:
>
> "the cat is in the box, or the cat is in the cupboard, or the cat is in
> the kitchen".
>
>
> But that is not how Python works. In Python, you have to say:
>
> cat in box or cat in cupboard or cat in kitchen
>
>
> Although this will work as well:
>
> any(cat in place for place in (box, cupboard, kitchen))
>
>
> In Python, an expression like this:
>
> cat in (box or cupboard or kitchen)
>
>
> has a completely different meaning. First, the expression in the round
> brackets is evaluated:
>
> (box or cupboard or kitchen)
>
>
> and then the test is performed:
>
> cat in (result of the above)
>
>
> The expression (box or cupboard or kitchen) means "return the first one
> of box, cupboard, kitchen that is a truthy value, otherwise the last
> value". Truthy values are those which are considered to be "like True":
>
> truthy values:
>
> - True
> - object()
> - numbers apart from zero
> - non-empty strings
> - non-empty lists
> - non-empty sets
> - non-empty dicts
> - etc.
>
> falsey:
>
> - False
> - None
> - zero (0, 0.0, Decimal(0), Fraction(0), etc.)
> - empty string
> - empty list
> - empty set
> - empty dict
> - etc.
>
> (Can you see the pattern?)
>
>
> So you can experiment with this yourself:
>
> 42 or 23 or "foo"
> => the first object is truthy, so it is returned
>
> 0 or 23 or "foo"
> => the first object is falsey, and the second object is truthy,
> so it is returned
>
> 0 or [] or "foo"
> => the first two objects are falsey, so the third is returned
>
>
> The "and" operator works in a similar fashion. Experiment with it and see
> how it works for yourself.

I read yours psots many times,all of them, tryign to understand them.


		if '-' not in ( name and month and year ):
			cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM 
clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = 
%s ORDER BY lastvisit ASC''', (name, month, year) )
		elif '-' not in ( name and year ):
			cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM 
clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit 
ASC''', (name, year) )
		elif '-' not in ( month and year ):
			cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit) = %s and 
YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
		elif '-' not in year:
			cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) = %s ORDER 
BY lastvisit ASC''', year )

======================

i just want 4 cases to examine so correct execute to be run:

i'm reading and reading and reading this all over:

if '-' not in ( name and month and year ):

and i cant comprehend it.

While it seems so beutifull saying:

if character '-' aint contained in string name , neither in string month 
neither in string year.

But it just doesn't work like this.

Since  ( name and month and year ) are all truthy values, what is 
returned by this expression to be checked if it cotnains '=' within it?

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


#47945

FromSibylle Koczian <nulla.epistola@web.de>
Date2013-06-13 14:22 +0200
Message-ID<mailman.3184.1371126194.3114.python-list@python.org>
In reply to#47913
Am 13.06.2013 09:11, schrieb Νικόλαος Κούρας:
> On 13/6/2013 4:55 πμ, Steven D'Aprano wrote:
>>
>> The "and" operator works in a similar fashion. Experiment with it and see
>> how it works for yourself.
>
> I read yours psots many times,all of them, tryign to understand them.
>

But you didn't do what he recommended, did you? And it's really the best 
or possibly the only way to understanding.

Try it out in the interactive Shell, using Stevens examples connected 
with 'and' instead of 'or'.

Sibylle



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


#47969

FromΝικόλαος Κούρας <support@superhost.gr>
Date2013-06-13 17:26 +0300
Message-ID<kpckqb$28k2$3@news.ntua.gr>
In reply to#47945
On 13/6/2013 3:22 μμ, Sibylle Koczian wrote:
> Am 13.06.2013 09:11, schrieb Νικόλαος Κούρας:
>> On 13/6/2013 4:55 πμ, Steven D'Aprano wrote:
>>>
>>> The "and" operator works in a similar fashion. Experiment with it and
>>> see
>>> how it works for yourself.
>>
>> I read yours psots many times,all of them, tryign to understand them.
>>
>
> But you didn't do what he recommended, did you? And it's really the best
> or possibly the only way to understanding.
>
> Try it out in the interactive Shell, using Stevens examples connected
> with 'and' instead of 'or'.

I try and try to work it out but i can't understand it even in theory.

==================

         if '-' not in ( name and month and year ):
             cur.execute( '''SELECT * FROM works WHERE clientsID = 
(SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and 
YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (name, month, year) )
         elif '-' not in ( name and year ):
             cur.execute( '''SELECT * FROM works WHERE clientsID = 
(SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER 
BY lastvisit ASC''', (name, year) )
         elif '-' not in ( month and year ):
             cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit) 
= %s and YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
         elif '-' not in year:
             cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) = 
%s ORDER BY lastvisit ASC''', year )

======================

i just want 4 cases to examine so correct execute to be run:

i'm reading and reading and reading this all over:

if '-' not in ( name and month and year ):

and i cant comprehend it.

While it seems so beautiful saying:

if character '-' ain't contained in string name , neither in string 
month neither in string year.

But it just doesn't work like this.

Since  ( name and month and year ) are all truthy values, what is 
returned by this expression to be checked if it cotnains '=' within it?


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


#48046

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-14 01:14 +0000
Message-ID<51ba6e92$0$29997$c3e8da3$5496439d@news.astraweb.com>
In reply to#47969
On Thu, 13 Jun 2013 17:26:18 +0300, Νικόλαος Κούρας wrote:

> i just want 4 cases to examine so correct execute to be run:
> 
> i'm reading and reading and reading this all over:
> 
> if '-' not in ( name and month and year ):
> 
> and i cant comprehend it.

Don't just read it. Open the interactive interpreter and test it.

name = "abcd"
month = "efgh"
year = "ijkl"

print(name and month and year)

If you run that, you will see what the result of 
(name and month and year) is. Now, ask yourself:

"k" in (name and month and year)

True or false? Check your answer:

print("k" in (name and month and year))



> While it seems so beautiful saying:
> 
> if character '-' ain't contained in string name , neither in string
> month neither in string year.
> 
> But it just doesn't work like this.

Correct. It doesn't work that way.

> Since  ( name and month and year ) are all truthy values, what is
> returned by this expression to be checked if it cotnains '=' within it?

Stop asking these questions. Try it for yourself and learn for yourself. 
You have a Python interactive interpreter. Try things, and see what they 
do. Read the documentation. THEN, and ONLY after you have done these 
things, should you ask for help.


-- 
Steven

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


#48077

FromNick the Gr33k <support@superhost.gr>
Date2013-06-14 11:03 +0300
Message-ID<kpeio7$p37$7@news.ntua.gr>
In reply to#48046
On 14/6/2013 4:14 πμ, Steven D'Aprano wrote:
> On Thu, 13 Jun 2013 17:26:18 +0300, Νικόλαος Κούρας wrote:
>
>> i just want 4 cases to examine so correct execute to be run:
>>
>> i'm reading and reading and reading this all over:
>>
>> if '-' not in ( name and month and year ):
>>
>> and i cant comprehend it.
>
> Don't just read it. Open the interactive interpreter and test it.
>
> name = "abcd"
> month = "efgh"
> year = "ijkl"
>
> print(name and month and year)
>
> If you run that, you will see what the result of
> (name and month and year) is. Now, ask yourself:
>
> "k" in (name and month and year)
>
> True or false? Check your answer:
>
> print("k" in (name and month and year))


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

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

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


#48083

FromChris Angelico <rosuav@gmail.com>
Date2013-06-14 18:23 +1000
Message-ID<mailman.3267.1371198232.3114.python-list@python.org>
In reply to#48077
On Fri, Jun 14, 2013 at 6:03 PM, Nick the Gr33k <support@superhost.gr> wrote:
>>>> print(name or month or year)
>>>> print("k" in (name and month and year))

Seriously, you can't see the difference between those lines? Either
you're trolling - which is still a distinct possibility, but so is the
converse - or there's something between here and the northern
hemisphere that makes the obvious unobvious and vice versa. Or maybe
I'm actually living in Alice's Wonderland and nothing here makes
sense...

ChrisA

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


#48084

From"R. Michael Weylandt" <michael.weylandt@gmail.com>
Date2013-06-14 09:24 +0100
Message-ID<mailman.3268.1371198287.3114.python-list@python.org>
In reply to#48077
On Fri, Jun 14, 2013 at 9:03 AM, Nick the Gr33k <support@superhost.gr> wrote:>
>>>> 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' ?

No it's not. See both above (where you use 'or' instead) and below
where _you yourself_ show that it's not 'abcd.'

Now read: https://en.wikipedia.org/wiki/Short-circuit_evaluation
noting especially the specified behavior for Python. If you find it
too technical, google for other uses of the terms.

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

Think about basic logic: 'or' means 'is at least one true?' so Python
only has to look at the first 'truthy value'. 'and' means 'are they
all true?' so Python has to look at all the values, ending up with the
last one, unless a 'falsey value' is found before.

Michael

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


#48085

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-06-14 11:28 +0300
Message-ID<qot1u85gkpq.fsf@ruuvi.it.helsinki.fi>
In reply to#48077
Nick the Gr33k writes:

>  >>> 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' ?

Why shouldn't (name or month or year) be different from (name and
month and year)?

Incidentally, you get better information without the print():

>>> 'Parker' and 'May' and '2001'
'2001'
>>> 

>>> 'Parker' and 'May' and 2001
2001
>>> 

Either way, the interactive prompt is your friend.

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


#48089

FromNick the Gr33k <support@superhost.gr>
Date2013-06-14 11:41 +0300
Message-ID<kpel04$spl$2@news.ntua.gr>
In reply to#48085
On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:

>>>> 'Parker' and 'May' and '2001'
> '2001'

But why?

that expression should return True since all stings are not empty.

> Either way, the interactive prompt is your friend.
>


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

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


#48092

FromChris Angelico <rosuav@gmail.com>
Date2013-06-14 18:50 +1000
Message-ID<mailman.3272.1371199807.3114.python-list@python.org>
In reply to#48089
On Fri, Jun 14, 2013 at 6:41 PM, Nick the Gr33k <support@superhost.gr> wrote:
> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
>
>>>>> 'Parker' and 'May' and '2001'
>>
>> '2001'
>
>
> But why?
>
> that expression should return True since all stings are not empty.

It does. Not the bool value, but it does return a true value.

ChrisA

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


#48095

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

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

On 14 Jun 2013 09:51, "Nick the Gr33k" <support@superhost.gr> wrote:
>
> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
>
>>>>> 'Parker' and 'May' and '2001'
>>
>> '2001'
>
>
> But why?
>
> that expression should return True since all stings are not empty.
>
>
>> Either way, the interactive prompt is your friend.
>>
>

At the very least read the replies to your questions.
http://code.activestate.com/lists/python-list/644572/

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


#48100

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-06-14 12:21 +0300
Message-ID<qotobb9f3oy.fsf@ruuvi.it.helsinki.fi>
In reply to#48089
Nick the Gr33k writes:

> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
> 
> >>>> 'Parker' and 'May' and '2001'
> > '2001'
> 
> But why?
> 
> that expression should return True since all stings are not empty.

It returns a value that counts as true in a conditional statement or
expression:

>>> if '2001': print('got a truish value')
... else: print("didn't")
... 
got a truish value

>>> if '': print('got a truish value')
... else: print("didn't")
... 
didn't

Zeroes and empty things tend to count as false in Python, other values
as true. The values are tested as is, not coerced to a boolean first,
so the value that decides the value of the whole expression is the
value of the whole expression.

>>> '' and whatever
''
>>> False and whatever
False
>>> 0 and whatever
0
>>> 1 and whatever
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'whatever' is not defined

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


#48104

FromNick the Gr33k <support@superhost.gr>
Date2013-06-14 12:44 +0300
Message-ID<kpeomq$spl$6@news.ntua.gr>
In reply to#48100
On 14/6/2013 12:21 μμ, Jussi Piitulainen wrote:
> Nick the Gr33k writes:
>
>> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
>>
>>>>>> 'Parker' and 'May' and '2001'
>>> '2001'
>>
>> But why?
>>
>> that expression should return True since all stings are not empty.
>
> It returns a value that counts as true in a conditional statement or
> expression:

When a look at ('Parker' and 'May' and '2001') i can't help it but 
interpret it as:

Return True if  'Parker' is not an empty string AND 'May' is not an 
empty string AND'2001'  is not an empty string.

Why on eart doesn't work this way?

I can understand that the value it results to '2000' is a truthy value, 
although i was expecting it to result in True if all parts of 
expressions are true.

i just don't understand why it returns back the last value instead.

> Zeroes and empty things tend to count as false in Python, other values
> as true. The values are tested as is, not coerced to a boolean first,
> so the value that decides the value of the whole expression is the
> value of the whole expression.
>
>>>> '' and whatever
> ''

Why does it return th first object back
isn't it like saying False and True and resulting in False?

Please put it in else word how Python unerstand that.
>>>> False and whatever
> False

Same here? The 2nd part of the expression never is been calculated 
because the 1st is False?
>>>> 0 and whatever
> 0

Same here? The 2nd part of the expression never is been calculated 
because the 1st is False?
>>>> 1 and whatever
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> NameError: name 'whatever' is not defined
>


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

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


#48132

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-06-14 15:40 +0300
Message-ID<qotmwqsq31b.fsf@ruuvi.it.helsinki.fi>
In reply to#48104
Nick the Gr33k writes:
> On 14/6/2013 12:21 μμ, Jussi Piitulainen wrote:
> > Nick the Gr33k writes:
> >> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
> >>
> >>>>>> 'Parker' and 'May' and '2001'
> >>> '2001'
> >>
> >> But why?
> >>
> >> that expression should return True since all stings are not empty.
> >
> > It returns a value that counts as true in a conditional statement or
> > expression:
> 
> When a look at ('Parker' and 'May' and '2001') i can't help it but
> interpret it as:
> 
> Return True if  'Parker' is not an empty string AND 'May' is not an
> empty string AND'2001'  is not an empty string.

Nah. That expression would be:

  True if ('Parker' != '' and 'May' != '' and '2001' != '') else False

The one at hand is more like: 'Parker' if 'Parker' does not count as
true, else 'May' if 'May' does not count as true, else '2001' (which
counts as true if it counts as true, else it counts as false ...).

> i just don't understand why it returns back the last value instead.

I suppose the value can be useful, and there is generally no harm in
it. But why not adjust your expectations to the reality? You can still
ask why.

This behaviour of the /and/ and /or/ was used to simulate the
conditional expression (_ if _ else _) before the latter was in the
language.

> >>>> '' and whatever
> > ''
> 
> Why does it return th first object back
> isn't it like saying False and True and resulting in False?
> 
> Please put it in else word how Python unerstand that.

That would be: '' if '' counts as false, else whatever.

And yes, when the first expression determines the value, the second
expression is not evaluated. /or/ is similar.

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


#48143

FromNick the Gr33k <support@superhost.gr>
Date2013-06-14 16:07 +0300
Message-ID<kpf4jc$spl$15@news.ntua.gr>
In reply to#48132
On 14/6/2013 3:40 μμ, Jussi Piitulainen wrote:
> Nick the Gr33k writes:
>> On 14/6/2013 12:21 μμ, Jussi Piitulainen wrote:
>>> Nick the Gr33k writes:
>>>> On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:
>>>>
>>>>>>>> 'Parker' and 'May' and '2001'
>>>>> '2001'
>>>>
>>>> But why?
>>>>
>>>> that expression should return True since all stings are not empty.
>>>
>>> It returns a value that counts as true in a conditional statement or
>>> expression:
>>
>> When a look at ('Parker' and 'May' and '2001') i can't help it but
>> interpret it as:
>>
>> Return True if  'Parker' is not an empty string AND 'May' is not an
>> empty string AND'2001'  is not an empty string.
>
> Nah. That expression would be:
>
>    True if ('Parker' != '' and 'May' != '' and '2001' != '') else False
>
> The one at hand is more like: 'Parker' if 'Parker' does not count as
> true, else 'May' if 'May' does not count as true, else '2001' (which
> counts as true if it counts as true, else it counts as false ...).
>
>> i just don't understand why it returns back the last value instead.
>
> I suppose the value can be useful, and there is generally no harm in
> it. But why not adjust your expectations to the reality? You can still
> ask why.
>
> This behaviour of the /and/ and /or/ was used to simulate the
> conditional expression (_ if _ else _) before the latter was in the
> language.
>
>>>>>> '' and whatever
>>> ''
>>
>> Why does it return th first object back
>> isn't it like saying False and True and resulting in False?
>>
>> Please put it in else word how Python unerstand that.
>
> That would be: '' if '' counts as false, else whatever.
>
> And yes, when the first expression determines the value, the second
> expression is not evaluated. /or/ is similar.
>

Thanks for explaining this but i cannot follow its logic at all.
My mind is stuck trying to interpret it as an English sentence:

if ('Parker' and 'May' and '2001')

if ('Parker' or 'May' or '2001')

i just don't get it and i feel silly about it.

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

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


Page 1 of 6  [1] 2 3 4 5 6  Next page →

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


csiph-web