How can I implement this logic more simply?
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?
How can I implement this logic more simply?
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?
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
I guess you are looking for a solution similar o isfemale_bit?'F':'M'
in C code
So you can use the and-or
construction (see Dive Into Python)
print isfemale_bit and 'F' or 'M'