Boolean Operations

official link:
https://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not
https://docs.python.org/2.7/reference/expressions.html#boolean-operations

Boolean Operations

These are the Boolean operations, ordered by ascending priority:


image.png

Notes:

This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.

image.png

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets, and frozensets). All other values are interpreted as true. (See the __nonzero__() special method for a way to change this.)

The operator not yields True if its argument is false, False otherwise.

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression xor y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

(Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g., not 'foo' yields False, not ''.)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吮铭,一起剝皮案震驚了整個濱河市蜂绎,隨后出現(xiàn)的幾起案子轮听,更是在濱河造成了極大的恐慌,老刑警劉巖杯拐,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件霞篡,死亡現(xiàn)場離奇詭異,居然都是意外死亡藕施,警方通過查閱死者的電腦和手機寇损,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來裳食,“玉大人矛市,你說我怎么就攤上這事』寤觯” “怎么了浊吏?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長救氯。 經(jīng)常有香客問我找田,道長,這世上最難降的妖魔是什么着憨? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任墩衙,我火速辦了婚禮,結(jié)果婚禮上甲抖,老公的妹妹穿的比我還像新娘漆改。我一直安慰自己,他們只是感情好准谚,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布挫剑。 她就那樣靜靜地躺著,像睡著了一般柱衔。 火紅的嫁衣襯著肌膚如雪樊破。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天唆铐,我揣著相機與錄音哲戚,去河邊找鬼。 笑死艾岂,一個胖子當著我的面吹牛顺少,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播澳盐,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼祈纯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了叼耙?” 一聲冷哼從身側(cè)響起腕窥,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎筛婉,沒想到半個月后簇爆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡爽撒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年入蛆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片硕勿。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡哨毁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出源武,到底是詐尸還是另有隱情扼褪,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布粱栖,位于F島的核電站话浇,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏闹究。R本人自食惡果不足惜幔崖,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望渣淤。 院中可真熱鬧赏寇,春花似錦、人聲如沸砂代。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽刻伊。三九已至露戒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間捶箱,已是汗流浹背智什。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留丁屎,地道東北人荠锭。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像晨川,于是被迫代替她去往敵國和親证九。 傳聞我的和親對象是個殘疾皇子删豺,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容