As the other answers state, Python's version for this is:
i if i < x else x
(of course for this particular example, one would prefer writing
min(i, x)
, as it is easier on the eyes)
However, this expression syntax was just made available on Python 2.5 (I think it was around 2004). before that, the following idiom was used - but care should be taken, as it is error prone:
i < x and i or x
- because the logical operator "and" actually evaluates to the last true value on its chain - therefore, if the expression was true, i < x and i
evaluates to i
- and the or
operator evaluates to first true value on the chain. On this case, if i < x
would be false, so would i< x and i
and the first true value would be x
.
It is easy to see how error prone this construct was, since if the boolean value of i
would be false ( for example if i==0), than it would return x, even if i < x
where true.
I myself, back in those days, used to write this construct instead:
(x, i)[i < x]
The expression "i < x" ha a numerical value of "1" (true) or "0" (false) and I used this proeprty to have it work as an index to the previous tuple. The problem with this approach was that it would always evaluate both expressions on the tuple, even though it would use ony one of then (if the expressions where function calls this could get expensive, or even conflicting)