01 Python 邏輯去運算

來源:互聯網
上載者:User

標籤:pytho   布爾值   python   返回   優先順序   布爾   bool   als   邏輯   

#and or not

#優先順序 ()>not>and>or

#and or notprint(2>1 and 1<4 or 2<3 and 9>6 or 2<4 and 3<2)#         True     or    True    or     False#True
print(3>4 or 4<3 and 1==1)                               #Falseprint(1<2 and 3<4 or 1>2)                                #Trueprint(2>1 and 3<4 or 4>5 and 2<1)                        #Trueprint(1>2 and 3<4 or 4>5 and 2>1 or 9<8)                 #Falseprint(1>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6)         #Falseprint(not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6)     #False

數字與布爾值轉換

#int-->boolprint(bool(2))   #Trueprint(bool(-2))  #Trueprint(bool(0))   #False#bool-->intprint(int(True))   #1print(int(False))  #0

x or y,若 x 為非0,則返回 x

print(1 or 2)    #1print(3 or 2)    #3print(0 or 2)    #2print(0 or 100)  #100

x and y,若 x 為真,則返回 y

print(1 and 2)   #2print(0 and 2)   #0

 #and or 組合

print(2 or 100 or 3 or 4)  #2print(0 or 4 and 3 or 2)   #3

#綜合 第一個為布爾型則返回布爾型,第一個為數值型,則返回數值型

print(1>2 and 3 or 4 and 3<2) #Falseprint(2 or 1<3 and 2)         #2

 

print(0 or 5<4)               #Falseprint(0 and 3>1)              #0print(3>1 and 0)              #0print(3>1 and 2 or 2<3 and 3 and 4 or 3>2)  #2

 

01 Python 邏輯去運算

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.