35

If you have an if statement where several variables or functions are evaluated, in which order are they evaluated?

if foo > 5 or bar > 6:
    print 'foobar'

In this specific case, will foo be evaluated against the five and then bar against the 6 (left to right) or will it be evaluated right to left? I am assuming that a or and and is evaluated in the same order.

Mogget
  • 834
  • 2
  • 11
  • 21
  • 3
    This has nothing to do with the order of evaluation of an `if` statement, but with the order of evaluation of an `or` expression. You get the exact same rules for `x = (foo > 5 or bar > 6)`, or, for that matter, just `foo > 5 or bar > 6` as a statement on its own. – abarnert Apr 24 '13 at 20:22
  • 3
    Try playing around and see what happens. Like what would be the outcome of this ;) `if True or False and False and False or False:` –  Apr 24 '13 at 20:25

5 Answers5

66

The left clause will be evaluated first, and then the right one only if the first one is False.

This is why you can do stuff like:

if not person or person.name == 'Bob':
    print "You have to select a person and it can't be Bob"

Without it breaking.

Conversely, with an and clause, the right clause will only be evaluated if the first one is True:

if person and person.name:
   # ...

Otherwise an exception would be thrown when person is None.

ubik
  • 4,440
  • 2
  • 23
  • 29
  • This is such an insightful answer. Does anyone know a collective reference containing such kind of trap-evading tips? – RyanC Mar 15 '22 at 07:59
13

To expand Blender's explanation a bit further, the or operator has something else built-in:

<expression A> or <expression B>

This will evaluate expression A first; if it evaluates to True then expression A is returned by the operator. So 5 or <something> will return 5 as 5 evaluates to True.

If expression A evaluates to False, expression B is returned. So 0 or 5 will return 5 because 0 evaluates to False.

Of course you can chain this as much as you want:

<expr 1> or <expr 2> or <expr 3> or ... or <expr n>

In general, or will return the first expression which evaluates to True, but keep its original value. If there is no expression that evaluates to True, it will simply return the last expression (which evaluates to False).

The and operator works in a similar but inversed way. It will return the first expression which does evaluate to False, but keep its original value. If there is no expression that evaluates to False, it will simply return the last expression (which will evaluate to True).

As an example, both 0 and 5 and 5 and 0 will return 0 because 0 evaluates to False, but 2 and 3 will return 3 because 3 is the last expression and everything evaluates to True.

In any way (to come back to the question): All expressions are evaluated from left to right, and if a rule from above allows it, further expressions will not be touched.

poke
  • 369,085
  • 72
  • 557
  • 602
11

It will be evaluated left to right.

>>> def a():
...     print 'a'
...     return False
... 
>>> def b():
...     print 'b'
...     return False
... 
>>> print a() or b()
a
b
False
>>> def c():
...     print 'c'
...     return True
... 
>>> print c() or a()
c
True
Nathan Villaescusa
  • 17,331
  • 4
  • 53
  • 56
2

Python's or operator short-circuits and it will be evaluated left to right:

if (foo > 5) or (bar > 6):
    print 'foobar'

If foo > 5, then bar > 6 will not even be tested, as True or <anything> will be True. If foo isn't > 5, then bar > 6 will be tested.

Blender
  • 289,723
  • 53
  • 439
  • 496
1

Operator evaluated from left to right. but only when the priority of all the operator is the same. When the priority of the operators is not equal then the operator will execute according to the priority.

ex.

False or False or True and False False

True or False and True or False True

False or False and True or False False

i.e First in the expression the operator having the highest priority executed then further execute.

Rahul Kumar
  • 11
  • 1
  • 2