这是Not
运算符的用法正确吗?
例如代码片段:
if not A or B:
这是否转化为if A is False or B is True
?
感谢您的帮助。
您应该查看操作的优先级。 对于这一部分:
not x
> and
> or
所以就像这里的if (not A) or B:
一样。
想想什么叫truth table
。另一个要理解的是 python 中的优先级。现在您了解了这一点,请尝试以下操作:
A = True
B = False
A or B # Test 1: always evaluates to True if one of them is True
A and B # Test 2: always evaluates to False is one of them is False
not A or B # Test 3 Negates Test 1(A or B) cause or is evaluated first
if A or B ==True:
print("One of the values in A or B is True")
else: print("Both are false")
我希望这有所帮助。你会没事的,我们都曾经在那里。祝你好运。