How do boolean operations work with parentheses in python 2.7?

ghz 昨天 ⋅ 3 views

Found this little oddity while playing around.

>>> 'Hello' == ('Hello' or 'World')
True
>>> 'Hello' == ('World' or 'Hello')
False
>>> 'Hello' == ('Hello' and 'World')
False
>>> 'Hello' == ('World' and 'Hello')
True

Is there some trick to this logic that I'm not getting? Why is the order of the strings the determining factor of these queries? Should I not be using parentheses at all? Why does changing to "and" flip the outputs?

Answer

This is an interesting case of operator precedence and the behavior of the or and and logical operators in Python. Let's break down what's going on here step by step.

1. or and and Operators:

  • or: The or operator returns the first truthy value it encounters. If the first value is truthy, it will return that value. If not, it will evaluate and return the second value.
  • and: The and operator returns the last truthy value. If the first value is falsy, it will return that value without evaluating the second. If both are truthy, it returns the second value.

2. Expression Breakdown:

Let's go through your examples:

1. 'Hello' == ('Hello' or 'World')

'Hello' == ('Hello' or 'World')
  • The or operator will check if the left-hand side ('Hello') is truthy. Since 'Hello' is non-empty (truthy), the result of the or operation is 'Hello'.
  • So this becomes:
    'Hello' == 'Hello'
    
  • The comparison is True, so the result is True.

2. 'Hello' == ('World' or 'Hello')

'Hello' == ('World' or 'Hello')
  • The or operator will check if the left-hand side ('World') is truthy. Since 'World' is also non-empty (truthy), the result of the or operation is 'World'.
  • So this becomes:
    'Hello' == 'World'
    
  • The comparison is False, so the result is False.

3. 'Hello' == ('Hello' and 'World')

'Hello' == ('Hello' and 'World')
  • The and operator checks if the left-hand side ('Hello') is truthy. Since 'Hello' is truthy, it proceeds to evaluate the second value ('World'), which is also truthy.
  • The result of the and operation is 'World' (the second operand).
  • So this becomes:
    'Hello' == 'World'
    
  • The comparison is False, so the result is False.

4. 'Hello' == ('World' and 'Hello')

'Hello' == ('World' and 'Hello')
  • The and operator checks if the left-hand side ('World') is truthy. Since 'World' is truthy, it evaluates the second value ('Hello'), which is also truthy.
  • The result of the and operation is 'Hello' (the second operand).
  • So this becomes:
    'Hello' == 'Hello'
    
  • The comparison is True, so the result is True.

3. Why Does the Order Matter?

The order matters because or and and work differently:

  • For or: The first value is returned if it's truthy, so in the expression 'Hello' or 'World', 'Hello' is returned because it is truthy. If you reverse the order like 'World' or 'Hello', 'World' is returned because it's the first truthy value.
  • For and: The second value is returned only if the first value is truthy, so in 'Hello' and 'World', the second operand ('World') is returned because both values are truthy. If you reverse the order like 'World' and 'Hello', the second operand ('Hello') is returned.

4. Should You Avoid Parentheses?

The parentheses in your expressions are not the problem — they simply group the or and and expressions explicitly. However, they do change the evaluation order in the expressions. When you don't use parentheses, the operators still have precedence, but parentheses clarify the grouping and operator order.

That said, if you're testing for equality (==) and want to control the logical evaluation, it's good practice to ensure you understand operator precedence and use parentheses to avoid unexpected behavior.

Summary of Key Points:

  1. or returns the first truthy value encountered.
  2. and returns the last truthy value encountered.
  3. Parentheses group expressions, but operator precedence is what ultimately governs the order of evaluation.
  4. Changing from or to and flips the behavior because the two operators are fundamentally different in how they return values.

In general, it's a good idea to use parentheses explicitly in complex expressions to make the order of operations clear.