3

With following condition, what should Python return? In which sequence the condition will get executed?

True or False and False and True

jwodder
  • 54,758
  • 12
  • 108
  • 124
Vivek
  • 33
  • 3

2 Answers2

3

AND has a higher precedence than OR.

True or False and False and True -> True or TRUE and True -> True or TRUE -> TRUE.

http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

  • Can you please explain bit more that in your first step, how did you derive -> True or True and True? As you previously mentioned that AND has a higher precedence hence my original question can be treated in either way as follows; True or (False and False) and True or True or False and (False and True) – Vivek Sep 21 '18 at 18:32
  • False and False is equal to False with a logical conjunction (AND); look here: https://en.wikipedia.org/wiki/Logical_conjunction – toom501 Sep 21 '18 at 18:33
  • 1
    The expression will be executed from left to right in the order of operators precedence. As @toom501 said below. I personally always use () when combining them. – Dean Dumitru Sep 21 '18 at 18:35
  • 2
    Also might want to note that in this example it'll actually short-circuit after seeing `True or` and return True regardless of what follows the `True or` – natonomo Sep 21 '18 at 20:31
1

Like @Dean Dumitru has said and like it said in this answer here; the AND has a higher precedence than OR.

Hence, your condition could be rewritten in this way:

True or ((False and False) and True) -> True or (False and True) -> True or False -> TRUE
toom501
  • 324
  • 1
  • 3
  • 15