Python中運算子not、and、or

來源:互聯網
上載者:User

標籤:add   style   sof   san   soft   hit   line   sed   運算子   

  • 優先順序

    1. and 與 2. or 或 3. not 非

  • 運算

    要記住:數字中非零為真零為假;True 為真 False 為假。

    or :與and相反,任意一個真即為真,同假才為假(因為要挨個查驗是否有真,所以假的情況下值為最後一個假值,例如:0 or False 為 False;False or 0 則為0。真的情況下值為第一個真值,例如:0 or 1 or 2 為 1;0 or False or 2 為 2


    and :與or相反,任意一個假即為假,同真為真(因為要挨個檢查是否有假,所以真的情況下值為最後一個真值,例如:True and 1 為 1;1 and True 則為True。假的情況下值為第一個假值,例如:0 and 1 and 2 為 0;True and False 為 False;True and 0 and False 為 0)


    not :對高優先順序的運算結果取反,值為布爾(2為真,取反為假:False)

  •       not 2:False   

          not 1 and 2:False

          not 1 or 2:False

          not not 1:True

          not 0 :True


    其實不只是Python中,所有的語言都是遵循這個邏輯的,面試題可能會考


    Python中運算子not、and、or

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.