the PHP ternary operator equivalent on Python

Steve M sjmaster at gmail.com
Fri Nov 18 16:54:28 EST 2005


Another way to simulate the ternary operator is this:

a = (quantity > 90 and "It is very huge") or "The value is correct"

You have to be careful of semantics of 'and' and 'or'. But in this case
I wonder why you don't just test whether quantity is greater than 90
and assign the corresponding value to a, e.g., :

if quantity > 90:
 a = "It is huge"
else:
 a = "Time for tea."


Clarity is a virtue, and simulating ternary operator doesn't always
serve that end.




More information about the Python-list mailing list