1、not關(guān)鍵詞可以反轉(zhuǎn)一個布爾值。
>>> not True
False
>>>
>>> not 1 > 0
False
2、當(dāng)使用if語句和while語句時,條件的結(jié)果會發(fā)生逆轉(zhuǎn)。
not關(guān)鍵字可以用于if語句:
當(dāng)條件不滿足時,上述if語句執(zhí)行代碼塊,但當(dāng)條件滿足時,它們不執(zhí)行,因為not關(guān)鍵取反了結(jié)果。
知識點擴(kuò)展:
python中的not具體表示是什么:
在python中not是邏輯判斷詞,用于布爾型True和False,not True為False,not False為True,以下是幾個常用的not的用法:
(1) not與邏輯判斷句if連用,代表not后面的表達(dá)式為False的時候,執(zhí)行冒號后面的語句。
比如:
a = False
if not a: (這里因為a是False,所以not a就是True)
print “hello”
這里就能夠輸出結(jié)果hello
(2) 判斷元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,這句話的意思是如果a不在列表b中,那么就執(zhí)行冒號后面的語句,比如:
a = 5
b = [1, 2, 3]
if a not in b:
print “hello”
這里也能夠輸出結(jié)果hello
not x 意思相當(dāng)于 if x is false, then True, else False
代碼中經(jīng)常會有變量是否為None的判斷,有三種主要的寫法:
第一種是if x is None;
第二種是 if not x:;
第三種是if not x is None(這句這樣理解更清晰if not (x is None))
以上就是python not關(guān)鍵字實例用法的詳細(xì)內(nèi)容,更多關(guān)于python not關(guān)鍵字的使用的資料請關(guān)注腳本之家其它相關(guān)文章!