zoukankan      html  css  js  c++  java
  • python 条件判断 if

    条件判断 if

    格式-1
    if 判断条件:
        执行语句...
    
    num = 10
    
    if num == 4:
        print("num 等于4")
    
    执行:
    C:Python27python.exe D:/Python/type-of-data.py
    end
    
    Process finished with exit code 0
    
    
    # 只输出end,原因 num等于10 经if判断 num不等于4 跳出if 判断执行判断外后面的代码
    ######################################################################################
    num = 10
    
    if num == 10:
        print("num 等于 10")
    print("end")
    执行:
    C:Python27python.exe D:/Python/type-of-data.py
    num 等于 10
    end
    
    Process finished with exit code 0
    
    输出
    两条print都输出.
    
    
    格式-2
    if 判断条件:
        执行语句...
    else:
        执行语句...
    
    num = 10
    
    if num == 4:
        print("num 等于 4")
    else:
        print("num 不等于 4")
    print("end")
    
    执行:
    C:Python27python.exe D:/Python/type-of-data.py
    num 不等于 4
    end
    
    Process finished with exit code 0
    
    # 当符合第一个条件时,执行第一个print语句,当不符合第一个条件时,执行else块中的语句
    
    格式-3(多层判断)
    if 判断条件:
        执行语句...
    elif 判断条件:
        执行语句...
    elif 判断条件:
        ...
        ...
    else:
        执行语句...
    
    num = 10
    
    if num == 4:
        print("num 等于 4")
    elif num == 5:
        print("num 等于 5")
    elif num == 0:
        print("num 等于 0")
    else:
        print("num 不等于 4 5 0")
    print("end")
    
    执行:
    C:Python27python.exe D:/Python/type-of-data.py
    num 不等于 4 5 0
    end
    
    Process finished with exit code 0
    
    # 代码会从if 开始对 if elif 的判断条进行匹配,当执行到匹配条件后执行对应的执行语句并不再执行后续的判断,当都不匹配时执行else对应的语句跳出判断执行执行判断外的代码
    
    
  • 相关阅读:
    整理:分页存储过程整理
    净利润-流通市值比率”与公司估值
    常见7种股票底部形态(图解)
    nginx
    移动成本分布1
    浅谈公开信息检索和判断能力
    股票技术分析 成交量与换手率专题
    成份股和成份股指数
    股票底部形态初探
    筹码拉抬派发法
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7733019.html
Copyright © 2011-2022 走看看