What is equivalent to Python x = (10 <n)? 10: n;
6 answers
Here is the ternary operator in Python (also known as conditional expressions in documents).
x if cond else y +7
There are various ways to do a triple operation, the first is an expression added with 2.5:
n = foo if condition else bar If you want to be compatible with versions below 2.5, you can use the fact that boolean are subclasses of int and that True behaves like 1 , while False behaves like 0 :
n = [bar, foo][condition] Another possibility is to use the way operators behave in Python or more precisely how and and or behave:
n = condition and foo or bar +4