Question presented to candidate:
"In Python you can write 0 <= x < 10. What does the interpreter actually do with that, and why is 3 > 2 > 1 True while (3 > 2) > 1 is False?"
What a strong answer should cover:
a < b < cmeans(a < b) and (b < c), not(a < b) < c; every comparison operator chains this way, including==,!=,is,is not,inandnot in.- The middle operand is evaluated once, operands are evaluated left to right, and the chain short-circuits:
cis never evaluated whena < bis false. - A chain only compares neighbours:
1 < 3 > 2is legal and True, and says nothing about how1and2relate. - Gotchas from mixing operators:
1 in [1, 2] == Trueis False,False == False in [False]is True. - CPython detail: the chain keeps the middle value on the stack (
SWAP/COPYindis) and callsbool()on each intermediate result, which is why NumPy arrays raise "truth value is ambiguous" in a chain.
Code / implementation expected: Optional β a range check, plus a side-effect function showing each operand is evaluated once.