Python中運算符not、and、or

  1. 優先級

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

  2. 運算

    要記住:數字中非零爲真零爲假;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中,所有的語言都是遵循這個邏輯的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章