equivalent of `a?b:c`

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?


啊三元运营商:

>>> print 'foo' if True else 'bar'
foo
>>> print 'foo' if False else 'bar'
bar

 print 'F' if isfemale_bit else 'M'
链接地址: http://www.djcxy.com/p/7392.html

上一篇: Python三元运算符

下一篇: 相当于`a?b:c`