Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108148
| From | Stephen Hansen <me+python@ixokai.io> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Conditionals And Control Flows |
| Date | 2016-05-04 11:54 -0700 |
| Message-ID | <mailman.389.1462388072.32212.python-list@python.org> (permalink) |
| References | <f12b6c42-8edb-4f91-8994-7c122e99461e@googlegroups.com> <1462388069.1988878.598232289.669D262F@webmail.messagingengine.com> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web