if not (1 > 2): #如果()中的表達式為假 print("hahaha") #1 > 2結果是假,所以執行hahaha else: print("hihihi")
結果:
hahaha
Process finished with exit code 0
if not 1: #1為真,並不為假,因此不執行hahaha,執行hihihi print("hahaha") else: print("hihihi")
結果:
hihihi
Process finished with exit code 0
python中非空即為真,空即為假,因此也常用來判斷變量是否為空
while(1): data = input() #輸入數據 if not data: #如果data為假(即data為空) print("hahaha") else: #如果data不為空 print("hihihi")
結果:
1
hihihi
2
hihihihahaha