equivalent of `a?b:c` [duplicate]
Possible Duplicate:
Python Ternary Operator
I want to print out a string in python. I don't want to do:
if isfemale_bit:
print 'F'
else:
print 'M'
The best I have right now is print ['M', 'F'][int(isfemale_bit)]
?
Is there a better alternative?
I needs my syntactic sugar!!
In Python 2.5, you can use ternary conditionals like this:
a if b else c
There is more discussion here: Does Python have a ternary conditional operator?
Ah the ternary operator:
>>> print 'foo' if True else 'bar'
foo
>>> print 'foo' if False else 'bar'
bar