Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
假设9 in list == False表达式是作为(9 in list) == False执行的,但事实并非如此。
相反,python将其计算为(9 in list) and (list == False),而后一部分永远不是真的。
应该是:
if (9 in list) == False: print "9 is not present in list"
这是由于comparison operator chaining。从文档中:
假设
9 in list == False
表达式是作为(9 in list) == False
执行的,但事实并非如此。相反,python将其计算为
(9 in list) and (list == False)
,而后一部分永远不是真的。你真的想使用
not in
运算符,并且避免给变量命名list
:相关问题 更多 >
编程相关推荐