pythonif結(jié)構(gòu)允許程序做出選擇,并根據(jù)不同的情況執(zhí)行不同的操作基本用法
比較運算符
根據(jù)PEP8標準,比較運算符兩側(cè)應該各有一個空格,比如:5==3。PEP8標準==(相等):如果該運算符兩側(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進行判斷
desserts=['icecream','chocolate','applecrisp','cookies']
favorite_dessert='applecrisp'
hate_dessert='chocolate'
fordessertindesserts:
ifdessert==favorite_dessert:
print("%sismyfavoritedessert!"%dessert.title())
以上內(nèi)容為大家介紹了pythonif判斷在哪寫,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構(gòu):千鋒教育。