I know there're some other articles talk about how to simulate ternary operator in Python, but my question here is not how can I simulate it, but why do these ways work.
As we all know simulating a ternary operator (condition: if_true ? if_false) is done by
a if condition else b
However, there're at least two other ways to accomplish that, e.g.
(if_false, if_true)[test]
and
(expression) and (if_true) or (if_false)
Example would be
(4, 5)[4 > 5]
gives 4
4 > 5 and 4 or 5
gives 5