zoukankan      html  css  js  c++  java
  • Python 学习笔记8 条件语句 If

    Python中条件语句if 是通过一条或者多条的执行语句的结果,来判断是否执行其包含的代码块。

    通常会配合else、elif一起使用,达到根据条件进行多个代码块的执行操作。

    简单的if 

    score = 90
    
    if score >= 95:
        print("优秀")
    
    #没有输出
    
    if 95 > score >= 80:
        print("")
    
    #输出: 良

    和else 配合使用:

    score = 90
    
    if score >= 60:
        print("合格")
    else:
        print("不合格")
    
    #输出: 合格

    使用if -elif-else判断结构:

    score = 80
    
    if score < 60:
        print("不合格")
    elif 80 > score >= 60:
        print("")
    else:
        print("")
    
    #输出: 良

     在if语句中嵌套条件语句:

    score = 80
    
    if score >= 60:
        print("通过")
        if 80 > score >= 60:
            print("")
        if 90 > score >= 80:
            print("")
        if 100 >= score >= 90:
            print("")
    else:
        print("不合格")
    
    #输出:通过
    #
    score = 80
    
    if score < 60:
        print("不合格")
    elif score < 70:
        print("合格")
    elif score < 80:
        print("")
    elif score < 90:
        print("")
    else:
        print("")
    
    #输出:良

    目前python3.0中没有 switch case结构,所以我们需要灵活的使用if-elif-else结构来进行条件判断。

  • 相关阅读:
    第七次
    第六次作业
    第五次作业
    第四次作业
    百度网盘生成二维码api
    【css3】--四种气泡
    纯CSS气泡框实现方法探究
    三种带箭头提示框总结实例
    纯CSS实现气泡框
    Windows 上 GitHub Desktop 的操作
  • 原文地址:https://www.cnblogs.com/wanghao4023030/p/10699090.html
Copyright © 2011-2022 走看看