Python boolean expression and or
Python boolean expression and or
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence
--
Chapters
00:00 Question
00:34 Accepted answer (Score 20)
01:53 Answer 2 (Score 6)
03:54 Answer 3 (Score 3)
05:05 Answer 4 (Score 2)
05:22 Thank you
--
Full question
https://stackoverflow.com/questions/3181...
Answer 1 links:
[the documentation]: https://docs.python.org/3/reference/expr...
Answer 2 links:
[Truth Value Testing]: http://docs.python.org/library/stdtypes....
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #syntax #booleanexpression
#avk47
ACCEPTED ANSWER
Score 20
The operators and and or are short-circuiting which means that if the result of the expression can be deduced from evaluating only the first operand, the second is not evaluated. For example if you have the expression a or b and a evaluates to true then it doesn't matter what b is, the result of the expression is true so b is not evaluated. They actually work as follows:
a and b: If a is falsey, b is not evaluated and a is returned, otherwise b is returned.a or b: If a is truthy, b is not evaluated and a is returned, otherwise b is returned.
Falsey and truthy refer to values that evaluate to false or true in a boolean context.
However this and/or idiom was useful back in the days when there was no better alternative, but now there is a better way:
spam if foo==bar else eggs
The problem with the and/or idiom (apart from it being confusing to beginners) is that it gives the wrong result if the condition is true but spam evaluates to a falsey value (e.g. the empty string). For this reason you should avoid it.
ANSWER 2
Score 6
This is how the Python boolean operators work.
From the documentation (the last paragraph explains why it is a good idea that the operators work the way they do):
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false:
False,None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. (See the__nonzero__()special method for a way to change this.)The operator
notyieldsTrueif its argument is false,Falseotherwise.The expression
x and yfirst evaluatesx; ifxis false, its value is returned; otherwise,yis evaluated and the resulting value is returned.The expression
x or yfirst evaluatesx; ifxis true, its value is returned; otherwise, y is evaluated and the resulting value is returned.(Note that neither
andnororrestrict the value and type they return toFalseandTrue, but rather return the last evaluated argument. This is sometimes useful, e.g., ifsis a string that should be replaced by a default value if it is empty, the expressions or 'foo'yields the desired value. Becausenothas to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g.,not 'foo'yieldsFalse, not''.)
ANSWER 3
Score 3
The reason is that Python evaluates boolean expression using the actual values of the variables involved, instead of restricting them to True and False values. The following values are considered to be false:
NoneFalse- 0 of any numeric type
- empty sequence or set (
'',(),[],{}) - user-defined types with
__nonzero__()or__len__()method that returns 0 orFalse
See the Truth Value Testing section of the Python documentation for more information. In particular:
Operations and built-in functions that have a Boolean result always return
0orFalsefor false and1orTruefor true, unless otherwise stated. (Important exception: the Boolean operationsorandandalways return one of their operands.)
ANSWER 4
Score 2
Try using parentheses to make the expression non-ambiguous. The way it is, you're getting:
(foo == bar and spam) or eggs