+ 1

Boolean priority?

Why is this output True? Does the Boolean ā€œandā€ have a priority over ā€œorā€? What am I not understanding? print(True or True and False) Output: True How I attempted to solve was by taking it apart from left to right. True or True = (True) (True) and False = False End result is False. Which is what I thought.

6th May 2022, 12:31 AM
Sal
2 Answers
+ 5
As OP have not mentioned any language, I am assuming it to be python and if that's the case then Yes, logical AND have higher precedence than OR, meaning the expression would be evaluated like this print ( True or ( True and False )) Which will always result in True Check the following docs to know more about operator precedence in pythonšŸ‘‡ https://docs.python.org/3/reference/expressions.html#operator-precedence
6th May 2022, 12:52 AM
Arsenic
Arsenic - avatar
+ 2
Yes, it was a python question! Thank you for the clarification.
6th May 2022, 12:58 AM
Sal