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对应的语句跳出判断执行执行判断外的代码
    
    
  • 相关阅读:
    堆和栈的区别 (转贴)
    Linux常用的网络命令
    H.264的编解码流程
    链表逆序
    快速排序
    一个计算机硕士毕业生的求职经验(五)
    H.264简单总结
    重要的热键 【Tab】,【Ctrl】—C,【Ctrl】—D
    Linux 文件与目录管理
    一个计算机硕士毕业生的求职经验(六)
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7733019.html
Copyright © 2011-2022 走看看