zoukankan      html  css  js  c++  java
  • python的if语句和while循环

    知识点整理:

    1.if语句:

            if 条件:
                代码块
            if 条件:
                代码块
            else:
                代码块
            if 条件:
                代码块
            elif 条件:
                代码块
            ....
            else:
                代码块
     
    2.while循环:
            while 条件:
                代码块(循环体)
            执行流程:
                1. 判断条件是否为真. 如果真. 执行代码块
                2. 再次判断条件是否为真......
                3. 当条件为假.执行else 跳出循环. 循环结束
     
    3.格式化输出:
            %s: 字符串的占位符, 可以放置任何内容(数字)
            %d: 数字的占位符
     
    4. 逻辑运算:
      and  并且的意思. 左右两端的值必须都是真. 运算结果才是真
      or   或者的意思. 左右两端有一个是真的. 结果就是真. 全部是假. 结果才能是假
      not  非的意思. 原来是假. 现在是真.  非真即假, 非假既真
      break   结束循环. 停止当前本层循环
      continue  结束当前本次循环. 继续执行下一次循环
     
    FAQ:
     
    str_1 = "中国"
    print("%s的人口占了全世界人口的%%30" % str_1)  #如果字符串中有了占位符. 那么后面的所有的%都是占位.需要再加一个%转义 !
    print("中国有的人口占全世界人口的%30")      #这句话中没有占位符 , %还是% !
     
    逻辑运算 :  and or not同时存在时 , 先算括号 , 然后算not , 再然后算and , 最后算or !
    print(1 < 2  and  3 < 4 or 1>2  ) # Ture
    print(2 > 1  and  3 < 4 or 4 > 5 and  2 < 1)    # Ture
    print(1 > 2  and  3 < 4 or 4 > 5 and  2 > 1  or 9 < 8) # False
    x or y 如果x==0 那么就是y, 否则是x
    print(1 or 2)   # 1
    print(2 or 3)   # 2
    print(0 or 3)   # 3
    print(0 or 4)   # 4
    print(0 or 1 or 3 or 0 or 5)   # 1
    x and y 如果x或y有一个为0则为0,否则为y
    print(1 and 2)  # 2
    print(2 and 0)  # 0
    print(0 and 3)  # 0
    print(0 and 4)  # 0
    print(0 or 4 and  3 or 7 or 9 and  6)   # 3
    0相当于False , 非0相当于Ture!
    print(2 > 3 and 3)   # False
    print(2 < 1 and 4 > 6 or 3 and 4 > 5 or 6)   # 6
    如果while循环后面跟了else,则while和else为一个整体!使用break会打断整体,不会执行后面的else!
    count  = 1
    while count  <= 10:
        print( count)
        count = count + 1
        if count == 5:
      break       # 彻底停止循环. 不会执行后面的else
    else:          # while条件不成立的时候执行
        print("这里是else")
     
     
     
     
     
     
  • 相关阅读:
    Javascript操作cookie
    上传文件-layui+ashx
    Mysql对查询结果添加序列号
    Maven详解
    解决 go iris ReadJSON 无法获取 json,并提示 unexpected end of JSON input 的错误
    Archlinux 最新安装方法 (2020.07.01-x86_64)之虚拟机 BIOS 安装
    Oracle APEX 发送邮件
    包管理Go module的使用
    Golang 解决 Iris 被墙的依赖包
    解决 Nginx 代理Apex慢的问题
  • 原文地址:https://www.cnblogs.com/Chou8p/p/9261533.html
Copyright © 2011-2022 走看看