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对应的语句跳出判断执行执行判断外的代码
    
    
  • 相关阅读:
    遮罩层点击空白退出代码
    不同浏览器的margin值与padding值
    让div自适应浏览器窗口居中显示
    导航相关(下方导航指示条居中)
    CSS相邻兄弟选择器
    使用font-size:0去掉inline-block元素之间的空隙
    box-sizing属性
    常见浏览器兼容性问题
    秋季编程总结
    POJ 1193 内存分配
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7733019.html
Copyright © 2011-2022 走看看