Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108133 > unrolled thread
| Started by | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| First post | 2016-05-04 07:41 -0700 |
| Last post | 2016-05-04 11:54 -0700 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.python
Conditionals And Control Flows Cai Gengyang <gengyangcai@gmail.com> - 2016-05-04 07:41 -0700
Re: Conditionals And Control Flows Bob Gailer <bgailer@gmail.com> - 2016-05-04 10:54 -0400
Re: Conditionals And Control Flows Michael Selik <michael.selik@gmail.com> - 2016-05-04 14:56 +0000
Re: Conditionals And Control Flows Chris Angelico <rosuav@gmail.com> - 2016-05-05 00:58 +1000
Re: Conditionals And Control Flows Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-05-04 18:10 +0300
Re: Conditionals And Control Flows Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-04 16:21 +0100
Re: Conditionals And Control Flows Cai Gengyang <gengyangcai@gmail.com> - 2016-05-04 08:23 -0700
Re: Conditionals And Control Flows Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-05-04 19:41 +0300
Re: Conditionals And Control Flows Stephen Hansen <me+python@ixokai.io> - 2016-05-04 11:54 -0700
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2016-05-04 07:41 -0700 |
| Subject | Conditionals And Control Flows |
| Message-ID | <f12b6c42-8edb-4f91-8994-7c122e99461e@googlegroups.com> |
I am trying to understand the boolean operator "and" in Python. It is supposed to return "True" when the expression on both sides of "and" are true For instance, 1 < 3 and 10 < 20 is True --- (because both statements are true) 1 < 5 and 5 > 12 is False --- (because both statements are false) bool_one = False and False --- This should give False because none of the statements are False bool_two = True and False --- This should give False because only 1 statement is True bool_three = False and True --- This should give False because only 1 statement is True bool_five = True and True --- This should give True because only 1 statement is True Am I correct ?
[toc] | [next] | [standalone]
| From | Bob Gailer <bgailer@gmail.com> |
|---|---|
| Date | 2016-05-04 10:54 -0400 |
| Message-ID | <mailman.385.1462373653.32212.python-list@python.org> |
| In reply to | #108133 |
On May 4, 2016 10:45 AM, "Cai Gengyang" <gengyangcai@gmail.com> wrote: > > I am trying to understand the boolean operator "and" in Python. It is supposed to return "True" when the expression on both sides of "and" are true > > For instance, > > 1 < 3 and 10 < 20 is True --- (because both statements are true) > 1 < 5 and 5 > 12 is False --- (because both statements are false) > > bool_one = False and False --- This should give False because none of the statements are False > bool_two = True and False --- This should give False because only 1 statement is True > bool_three = False and True --- This should give False because only 1 statement is True > bool_five = True and True --- This should give True because only 1 statement is True > > Am I correct ? Yes. Caveat : python Boolean operators return the value of the last argument necessary to determine the outcome. Example : 2 or 3 results in 2 2 and 3 results in 3. 0 and 3 results in 0. HTH.
[toc] | [prev] | [next] | [standalone]
| From | Michael Selik <michael.selik@gmail.com> |
|---|---|
| Date | 2016-05-04 14:56 +0000 |
| Message-ID | <mailman.386.1462373798.32212.python-list@python.org> |
| In reply to | #108133 |
On Wed, May 4, 2016 at 10:46 AM Cai Gengyang <gengyangcai@gmail.com> wrote: > I am trying to understand the boolean operator "and" in Python. It is > supposed to return "True" when the expression on both sides of "and" are > true > Not exactly, because they will short-circuit. Take a look at the docs. ( https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not )
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-05-05 00:58 +1000 |
| Message-ID | <mailman.387.1462373907.32212.python-list@python.org> |
| In reply to | #108133 |
On Thu, May 5, 2016 at 12:41 AM, Cai Gengyang <gengyangcai@gmail.com> wrote: > I am trying to understand the boolean operator "and" in Python. It is supposed to return "True" when the expression on both sides of "and" are true > > For instance, > > 1 < 3 and 10 < 20 is True --- (because both statements are true) > 1 < 5 and 5 > 12 is False --- (because both statements are false) > > bool_one = False and False --- This should give False because none of the statements are False > bool_two = True and False --- This should give False because only 1 statement is True > bool_three = False and True --- This should give False because only 1 statement is True > bool_five = True and True --- This should give True because only 1 statement is True > > Am I correct ? Not entirely so, but very close. So long as you stick to the exact values True and False (or simple conditional expressions, like your examples), yes, that's what you'd get. The best way to try these out is the interactive interpreter. On Windows, look in your Start menu for "IDLE"; on other platforms, open up a terminal and type "python3". Then just start messing around: >>> 1 < 3 and 10 < 20 True >>> 1 < 5 and 5 > 12 False This is far and away the easiest way to learn how Python works. You can even play with some other things, and learn how Python's 'and' operator handles other types of data: >>> 1 and 4 4 >>> 0 and 3 0 Confused? Keep messing around. Build up a theory as to what's going on, test your theory, then go check your theory against the documentation. (Or come and ask here, if you can't find it in the docs.) Python doesn't mind how much you poke around with it, and you will learn ever so much more than we can explain! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jussi.piitulainen@helsinki.fi> |
|---|---|
| Date | 2016-05-04 18:10 +0300 |
| Message-ID | <lf5oa8leu5l.fsf@ling.helsinki.fi> |
| In reply to | #108133 |
Cai Gengyang writes: > I am trying to understand the boolean operator "and" in Python. It is > supposed to return "True" when the expression on both sides of "and" > are true > > For instance, > > 1 < 3 and 10 < 20 is True --- (because both statements are true) Yes. > 1 < 5 and 5 > 12 is False --- (because both statements are false) No :) > bool_one = False and False --- This should give False because none of the statements are False > bool_two = True and False --- This should give False because only 1 statement is True > bool_three = False and True --- This should give False because only 1 statement is True Yes. > bool_five = True and True --- This should give True because only 1 statement is True No :) > Am I correct ? Somewhat. In a technical programming-language sense, these are "expressions", not "statements". Technically, if the first expression evaluates to a value that counts as true in Python, the compound expression "E and F" evaluates to the value of the second expression. Apart from False, "empty" values like 0, "", [] count as false in Python, and all the others count as true. But it's true that "E and F" only evaluates to a true value when both E and F evaluate to a true value. Your subject line is good: Python's "and" is indeed a conditional, control-flow operator.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-05-04 16:21 +0100 |
| Message-ID | <87pot1vofd.fsf@bsb.me.uk> |
| In reply to | #108138 |
Jussi Piitulainen <jussi.piitulainen@helsinki.fi> writes: > Cai Gengyang writes: > >> I am trying to understand the boolean operator "and" in Python. It is >> supposed to return "True" when the expression on both sides of "and" >> are true >> >> For instance, >> >> 1 < 3 and 10 < 20 is True --- (because both statements are true) > > Yes. > >> 1 < 5 and 5 > 12 is False --- (because both statements are false) > > No :) > >> bool_one = False and False --- This should give False because none >> of the statements are False >> bool_two = True and False --- This should give False because only 1 >> statement is True >> bool_three = False and True --- This should give False because only >> 1 statement is True > > Yes. > >> bool_five = True and True --- This should give True because only 1 >> statement is True > > No :) > >> Am I correct ? > > Somewhat. Just an observation on the language... Change "only 1 statement" to "only statement 1" and you get much closer to a correct explanation. <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Cai Gengyang <gengyangcai@gmail.com> |
|---|---|
| Date | 2016-05-04 08:23 -0700 |
| Message-ID | <27d8c2c9-304e-4d25-81fd-fb350cf913c1@googlegroups.com> |
| In reply to | #108138 |
Sorry I mistyped , this should be correct : bool_one = False and False --- This should give False because none of the statements are True bool_two = True and False --- This should give False because only 1 statement is True bool_three = False and True --- This should give False because only 1 statement is True bool_five = True and True --- This should give True because both statements are True On Wednesday, May 4, 2016 at 11:10:28 PM UTC+8, Jussi Piitulainen wrote: > Cai Gengyang writes: > > > I am trying to understand the boolean operator "and" in Python. It is > > supposed to return "True" when the expression on both sides of "and" > > are true > > > > For instance, > > > > 1 < 3 and 10 < 20 is True --- (because both statements are true) > > Yes. > > > 1 < 5 and 5 > 12 is False --- (because both statements are false) > > No :) > > > bool_one = False and False --- This should give False because none of the statements are False > > bool_two = True and False --- This should give False because only 1 statement is True > > bool_three = False and True --- This should give False because only 1 statement is True > > Yes. > > > bool_five = True and True --- This should give True because only 1 statement is True > > No :) > > > Am I correct ? > > Somewhat. > > In a technical programming-language sense, these are "expressions", not > "statements". Technically, if the first expression evaluates to a value > that counts as true in Python, the compound expression "E and F" > evaluates to the value of the second expression. > > Apart from False, "empty" values like 0, "", [] count as false in > Python, and all the others count as true. > > But it's true that "E and F" only evaluates to a true value when both E > and F evaluate to a true value. > > Your subject line is good: Python's "and" is indeed a conditional, > control-flow operator.
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jussi.piitulainen@helsinki.fi> |
|---|---|
| Date | 2016-05-04 19:41 +0300 |
| Message-ID | <lf5mvo5ydux.fsf@ling.helsinki.fi> |
| In reply to | #108140 |
Cai Gengyang writes: > Sorry I mistyped , this should be correct : > > bool_one = False and False --- This should give False because none of the statements are True > bool_two = True and False --- This should give False because only 1 statement is True > bool_three = False and True --- This should give False because only 1 statement is True > bool_five = True and True --- This should give True because both statements are True Yes. (That is, afterwards bool_one, bool_two, ... evaluate to the stated truth values, for the stated reasons.)
[toc] | [prev] | [next] | [standalone]
| From | Stephen Hansen <me+python@ixokai.io> |
|---|---|
| Date | 2016-05-04 11:54 -0700 |
| Message-ID | <mailman.389.1462388072.32212.python-list@python.org> |
| In reply to | #108133 |
On Wed, May 4, 2016, at 07:41 AM, Cai Gengyang wrote: > I am trying to understand the boolean operator "and" in Python. It is > supposed to return "True" when the expression on both sides of "and" are > true The thing is, its kinda dubious to think of 'and' as a 'boolean operator', because once you go down that road, some people start wanting it to be a *pure* boolean operator. Something that always returns True or False. Instead, 'and' and 'or' return something that is true, or something that is false. Notice the lower case. (I know the docs call them Boolean Operations, but I still think saying 'boolean' is unhelpful) Python defines false things as False, None, 0 (of any numeric type), an empty container (lists, tuples, mappings, something else that defines __len__ and it returns 0), and instances of classes that define __nonzero__ that return 0 or False. Everything else is a true thing. If you see "x and y", the rule is: if x is a false thing, it'll return something false. As it happens, it has x handy, and since its decided x is false, it'll return that. Therefore, "x and y" is false. If x is true, though, it'll return y. In this case, "x and y" will be a true thing if y is a true thing, and a false thing if y is a false thing. As you can see, all of this logic happens without ever using True or False. -- Stephen Hansen m e @ i x o k a i . i o
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web