gpt4 book ai didi

python - 如何在 Python 中获得 boolean 值的相反(否定)?

转载 作者:IT老高 更新时间:2023-10-28 21:41:32 35 4
gpt4 key购买 nike

对于以下示例:

def fuctionName(int, bool):
if int in range(...):
if bool == True:
return False
else:
return True

有没有办法跳过第二个 if 语句?只是告诉计算机返回与 boolean 值相反的bool

最佳答案

要否定 boolean 值,您可以使用 not operator :

not bool

或者在您的情况下, if/return block 可以替换为:

return not bool

请务必注意 operator precedence rules ,以及取反的 isin 运算符:a is not ba not in b .

关于python - 如何在 Python 中获得 boolean 值的相反(否定)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7030831/

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