python里的if語(yǔ)句寫法:
if結(jié)構(gòu)
if結(jié)構(gòu)允許程序做出選擇,并根據(jù)不同的情況執(zhí)行不同的操作
基本用法
比較運(yùn)算符
根據(jù)PEP8標(biāo)準(zhǔn),比較運(yùn)算符兩側(cè)應(yīng)該各有一個(gè)空格,比如:5==3。PEP8標(biāo)準(zhǔn)
==(相等):如果該運(yùn)算符兩側(cè)的值完全相同則返回True
!=(不等):與相等相反
print(5=='5')
print(True=='1')
print(True==1)
print('Eric'.lower()=='eric'.lower())
>(大于):左側(cè)大于右側(cè)則輸出True
<(小于):與大于相反
>=(大于等于):左側(cè)大于或者等于右側(cè)則輸出True
<=(小于等于):左側(cè)小于或者等于右側(cè)則輸出True
print(5>3)
print(2>True)
print(True>False)
if的用法
if進(jìn)行判斷
desserts=['icecream','chocolate','applecrisp','cookies']
favorite_dessert='applecrisp'
hate_dessert='chocolate'
fordessertindesserts:
ifdessert==favorite_dessert:
print("%sismyfavoritedessert!"%dessert.title())
以上內(nèi)容為大家介紹了python里的if語(yǔ)句怎么寫,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。