Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean operations

I'm confused on how Python evaluates boolean statements.

For ex.

False and 2 or 3

returns 3

How is this evaluated? I thought Python first looks at 'False and 2', and returns False without even looking at 'or 3'. What is the order of what Python sees here?

Another is:

1 or False and 2 or 2 and 0 or 0

returns 1

From what I gathered from the first example, I thought Python would evaluate from left to right, so '1 or False' would return 1, then '1 and 2' would return 2, then '2 or 2' would return the first 2, then '2 and 0' would return 0, then '0 or 0' would return the second 0.

As you can tell I'm pretty perplexed here, please help!

Thanks!

like image 777
kkSlider Avatar asked May 06 '12 22:05

kkSlider


People also ask

What are the 3 types of Boolean operations?

They connect your search words together to either narrow or broaden your set of results. The three basic boolean operators are: AND, OR, and NOT.

What is Boolean operation?

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. This should save time and effort by eliminating inappropriate hits that must be scanned before discarding.

What is a Boolean operator example?

The most common Boolean operators are AND, OR, NOT or AND NOT, quotation marks “”, parentheses (), and asterisks *.

What are Boolean operations in 3D modeling?

In 3D Modeling, by Boolean operations we mean creating intersections and unions of objects, as well as subtracting objects from each other. All these are set operations that students know from Venn diagrams.


2 Answers

and has higher precedence than or.

False and 2 or 3

is evaluated as

((False and 2) or 3)

Since the first part (False and 2) is False, Python has to evaluated the second part to see whether the whole condition can still become True or not. It can, since 3 evaluates to True so this operand is returned.

Similar for 1 or False and 2 or 2 and 0 or 0 which is evaluated as

(1 or ((False and 2) or ((2 and 0) or 0)))

Since 1 evaluates to True, the whole condition will be True, no matter which value the other operands have. Python can stop evaluating at this point and again, returns the operand that determines the final value.

Stopping as early as the final result is determined is called short-circuit evaluation and can be described as follows:

Whenever the final result of the expression is determined, the evaluation is stopped and in Python the value of the operand that determines the final value is returned. That is, assuming a left-to-right evaluation:

  • for the and operator, the left-most operand that evaluates to False (or the last one)
  • for the or operator, the left-most operand that evaluates to True (or the last one)
like image 194
Felix Kling Avatar answered Sep 21 '22 11:09

Felix Kling


Check out section 5.15 of this page: http://docs.python.org/reference/expressions.html

or has a lower precendence than and, so your statement is evaluated as

(false and 2) or 3
like image 22
Marc B Avatar answered Sep 21 '22 11:09

Marc B