if not a表示:判断a没有值(没有值返回Ture,有值返回False)
序 号 | 判断语句 | 备注 | a | |||||||
a=False | a=None | a=[ ] | a="" | a=" " | a=0 | a=1 | a=3 | |||
1 | if not a | 判断a没有值(没有值返回Ture,有值返回False) | True | True | True | True | False | True | False | False |
2 | if a is False | 判断a和False的id是否一致 | True | False | False | False | False | False | False | False |
3 | if not a is False | if not (a is False) | False | True | True | True | True | True | True | True |
4 | if a is not False | if a is not False | False | True | True | True | True | True | True | True |
5 | if a == False | 判断a和False的值和类型是否一致 | True | False | False | False | False | True | False | False |
6 | if not a == False | if not (a == False) | False | True | True | True | True | False | True | True |
7 | if a != False | False | True | True | True | True | False | True | True | |
8 | if a is None | 判断a和None的id是否一致 | False | True | False | False | False | False | False | False |
9 | if not a is None | if not (a is None) | True | False | True | True | True | True | True | True |
10 | if a is not None | if a is not None | True | False | True | True | True | True | True | True |
11 | if a == None | 判断a的值和类型是不是None | False | True | False | False | False | False | False | False |
12 | if not a == None | if not (a == None) | True | False | True | True | True | True | True | True |
13 | if a != None | True | False | True | True | True | True | True | True |
注意:
1. Python中False,Ture,None都是关键字,首字母大写,而false和FALSE不是。
2. 判断中is和==不是同一个意思,is判断的变量存储id是否相同,而==判断的 是变量的值和类型。
3. Python3 中bool 是 int 的子类,True 和 False 可以和数字相加, True==1、False==0 为True,但可以通过 is 来判断类型。Python2 中是没有布尔型的,它用数字 0 表示 False,用 1 表示 True。
下面是测试用的代码:
a = 3
print("########### a = 3 ###########")
if not a:
print("1.if not a,,,true")
else:
print("1.if not a,,,false")
######################################################################
if a is False:
print("2.if a is False,,,true")
else:
print("2.if a is False,,,false")
###################################
if not a is False:
print("3.if not a is False,,,true")
else:
print("3.if not a is False,,,false")
###################################
if a is not False:
print("4.if a is not False,,,true")
else:
print("4.if a is not False,,,false")
###################################
if a == False:
print("5.if a == False,,,true")
else:
print("5.if a == False,,,false")
###################################
if not a == False:
print("6.if not a == False,,,true")
else:
print("6.if not a == False,,,false")
###################################
if a != False:
print("7.if a != False,,,true")
else:
print("7.if a != False,,,false")
######################################################################
if a is None:
print("8.if a is None,,,true")
else:
print("8.if a is None,,,false")
###################################
if not a is None:
print("9.if not a is None,,,true")
else:
print("9.if not a is None,,,false")
###################################
if a is not None:
print("10.if a is not None,,,true")
else:
print("10.if a is not None,,,false")
###################################
if a == None:
print("11.if a == None,,,true")
else:
print("11.if a == None,,,false")
###################################
if not a == None:
print("12.if not a == None,,,true")
else:
print("12.if not a == None,,,false")
###################################
if a != None:
print("13if a != None,,,true")
else:
print("13if a != None,,,false")