日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python3.6中if語(yǔ)句怎么用

python的if語(yǔ)句為條件判斷語(yǔ)句,習(xí)慣與else搭配使用。

if 結(jié)構(gòu)允許程序做出選擇,并根據(jù)不同的情況執(zhí)行不同的操作

if的用法

1.只有 if 進(jìn)行判斷

desserts = ['ice cream', 'chocolate', 'apple crisp', 'cookies']
favorite_dessert = 'apple crisp'
hate_dessert = 'chocolate'

for dessert in desserts:
    if dessert == favorite_dessert:
        print("%s is my favorite dessert!" % dessert.title())

2. if - else 進(jìn)行判斷

for dessert in desserts:
    # 比較運(yùn)算符(== 相等 、!= 不等、> 大于、>= 大于等于、< 小于、<=小于等于)
    if dessert == favorite_dessert:
        print("%s is my favorite dessert!" % dessert.title())
        
    # elif => else + if 當(dāng)前值不符合上面 if 的判斷條件,執(zhí)行 elif 的判斷條件
    else:
        print("I like %s." % dessert)

3. if - elif - else 進(jìn)行判斷,其中 elif 不是唯一的,可以根據(jù)需要添加,實(shí)現(xiàn)更細(xì)粒度的判斷

# 對(duì)不同的 dessert 輸出不完全相同的結(jié)果
for dessert in desserts:
    # 比較運(yùn)算符(== 相等 、!= 不等、> 大于、>= 大于等于、< 小于、<=小于等于)
    if dessert == favorite_dessert:
        print("%s is my favorite dessert!" % dessert.title())
        
    # elif => else + if 當(dāng)前值不符合上面 if 的判斷條件,執(zhí)行 elif 的判斷條件
    elif dessert == hate_dessert:
        print("I hate %s." % dessert)
    # 當(dāng)前值不符合上面所有的判斷條件,就執(zhí)行 else 里的語(yǔ)句
    # 當(dāng)然如果這個(gè)else 不需要的話,可以不寫
    else:
        print("I like %s." % dessert)

值得注意的一點(diǎn)是:當(dāng)整個(gè) if 判斷滿足某一個(gè)判斷條件時(shí),就不會(huì)再繼續(xù)判斷該判斷條件之后的判斷

4.特殊的判斷條件

if 0: # 其他數(shù)字都返回 True
    print("True.")
else:
    print("False.") # 結(jié)果是這個(gè)
    
if '': #其他的字符串,包括空格都返回 True
    print("True.")
else:
    print("False.") # 結(jié)果是這個(gè)
    
if None: # None 是 Python 中特殊的對(duì)象  
    print("True.")
else:
    print("False.") # 結(jié)果是這個(gè)
    
if 1:
    print("True.") # 結(jié)果是這個(gè)
else:
    print("False.")

新聞標(biāo)題:創(chuàng)新互聯(lián)Python教程:python3.6中if語(yǔ)句怎么用
分享鏈接:http://m.5511xx.com/article/codsdec.html