Skip to content

Question 3

Evaluate:

0 or not 1 / (float(4//3)-4/3) and 1 / (int(4/3)-4//3)
Hint

Work out the expression from left to right in stages.

Solution

We can split the compound expression into 3 parts:

  • Part 1: 0
  • Part 2: not 1 / (float(4//3)-4/3)

    not 1 / (float(4//3)-4/3)
    = not 1 / (float(1)-4/3)
    = not 1 / (1.0 - 4/3)
    

    By this point we can deduce mathematically that: $ \frac{1}{1.0 - \frac{4}{3}} = -3 $ Technically 1 / (float(4//3)-4/3) will produce -3.000000000000001, but the exact value in this case does not matter as much. Because -3 is not a False like value/falsy value, not -3 or not -3.000000000000001 will produce False immediately.

  • Part 3: 1 / (int(4/3)-4//3)

    1 / (int(4/3)-4//3)
    = 1 / (int(1.3333333333333333)-4//3)
    = 1 / (1 - 4//3)
    = 1 / (1 - 1)
    = 1 / 0 # raises `ZeroDivisionError`
    

We now have 0 or False and 1/0. 0 or False evaluates to False, since neither LHS or RHS of this part of the compound expression does not evaluate to a truthy/True like value.

With False and 1/0, it evaluates to False immediately. While 1/0 will raise a ZeroDivisionError immediately, that is not the first part of the expression that is evaluated. Because the LHS of this expression is already known to evaluate to False, by short-hand logic, it produces False.

0 or False and 1/0  # evaluate (0 or False) first
= False and 1/0
= False
Answer
False