Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python boolean expression and or

Tags:

In python if you write something like

foo==bar and spam or eggs

python appears to return spam if the boolean statement is true and eggs otherwise. Could someone explain this behaviour? Why is the expression not being evaluated like one long boolean?

Edit: Specifically, I'm trying to figure out the mechanism why 'spam' or 'eggs' is being returned as the result of the expression.

like image 708
Zxaos Avatar asked Jul 05 '10 20:07

Zxaos


People also ask

Is Boolean and or or?

"What is a Boolean Operator?" Boolean Operators are simple words (AND, OR, NOT or AND NOT) used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results.

What are the 3 boolean operators in Python?

There are three logical operators that are used to compare values. They evaluate expressions down to Boolean values, returning either True or False . These operators are and , or , and not and are defined in the table below.


2 Answers

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.

like image 99
Mark Byers Avatar answered Oct 15 '22 18:10

Mark Byers


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 not yields True if its argument is false, False otherwise.

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

(Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not has 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' yields False, not ''.)

like image 21
codeape Avatar answered Oct 15 '22 18:10

codeape