(1) not与逻辑判断句if连用
eg:
a = False
if not a: (这里因为a是False,所以not a就是True)
print “hello”
(2)判断元素是否在列表或者字典
a = 5
b = [1, 2, 3]
if a not in b:
print “hello”
这里也能够输出结果hello
(3)判断是否为空:
代码中经常会有变量是否为None的判断,有三种主要的写法:
第一种是if x is None
;
第二种是 if not x:
;
第三种是if x is not None
区别:
if not x:判断的是x是否为空,也就是说里面有东西没
if x is None:判断的是A是否声明并定义
eg:
>>> A = []
>>> A is None
False # 因为A已经定义了,无论有无东西
>>> not A
True # A中为空