gpt4 book ai didi

python - 相当于 `a?b:c`

转载 作者:IT老高 更新时间:2023-10-28 21:46:48 25 4
gpt4 key购买 nike

Possible Duplicate:
Python Ternary Operator

我想在 python 中打印出一个字符串。我不想这样做:

if isfemale_bit:
print 'F'
else:
print 'M'

我现在最好的是 print ['M', 'F'][int(isfemale_bit)]?

有更好的选择吗?

我需要我的语法糖!!

最佳答案

在 Python 2.5 中,您可以像这样使用三元条件:

a if b else c

这里有更多讨论:Does Python have a ternary conditional operator?

关于python - 相当于 `a?b:c`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5995350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com