嗨,有人可以了解工作的机制吗?"" Python中的运算符。
我现在正在处理以下示例:
print ('a' not in ['a', 'b']) # outputs False
print (not 'a' in ['a', 'b']) # outputs False -- how ???
print ('c' not in ['a', 'b']) # outputs True
print (not 'c' in ['a', 'b']) # outputs True
print (not 'a') # outputs False
# ok is so then...
print (not 'a' in ['b', False]) # outputs True --- why ???
我现在很想知道它是怎么回事。如果有人知道,请分享您的知识。 谢谢=)
答案 0 :(得分:8)
in
has higher precedence than not
。因此,执行包容检查,然后在需要时否定结果。 'a'
不在['b', False]
,并且结果False
被否定导致True
。
答案 1 :(得分:2)
not
关键字基本上是"反转"这里返回布尔值。
对于第一个示例,a
位于数组中,因此“{1}}为真,但not true
为false。太错了。
对于第二个示例,a
不在数组中,因此错误,但not false
为真。真的。
答案 2 :(得分:0)
print (not 'a' in ['a', 'b'])
将其分解为:
not 'a'
自行评估False
(因为除了0,无,False,空列表和空字典外,任何内容都被视为True
)
且false
不在['a','b']
中
False in ['a','b']
评估为False
并且最后一个not 'a'
评估为False
,因此False in ['b', False]
评估为True