Question 3
The following Python expression is entered into a fresh Python shell with no prior import statements. Determine the result from evaluating the following expression. [3 marks]
Solution
Both or and and operators have the same precedence level, hence we work through this expression from left to right.
However, note that from the get-go, we pit True against the rest of the expression (i.e., 'p'*2 and 1 > 3 or 1 / 0).
This means that regardless of whatever 'p'*2 and 1 > 3 or 1 / 0 evaluates to, which does contain at least one False condition (1 > 3) and one that results in a ZeroDivisionError (1 / 0), having the left-hand side be True in a compound statement with an or operator results in short-circuit logic being applied.
- LHS:
True - RHS:
'p'*2 and 1 > 3 or 1 / 0\(\Rightarrow\) (no need to evaluate)
\(\therefore\) LHS or RHS = True