1. 程式人生 > >Python中運算符not、and、or

Python中運算符not、and、or

true 運算符 nbsp pytho false 情況下 color tro 布爾

  • 優先級

    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