zoukankan      html  css  js  c++  java
  • day02

    1. 流程控制 while 循环

    while 条件:
      代码块(循环体)
    else:   # while不成立的时候执行else
    执行流程:   
    1. 判断条件是否为真. 如果真,执行代码块.   2. 再次判断条件是否为真.....   3. 当条件为假,跳出循环,循环结束.

    那我们怎么终⽌循环呢?
      结束循环:
        1.改变条件
        2.break

    # 示例1: 喷人8次
    count
    = 1 # 计数 while count <= 8: print("你是alex么?") print("你全家都是太白") count = count + 1 # 每循环一次,count加1
    # 示例2: 让用户尽情的喷. 出入q退出
    
    while True:
        s = input("请开始喷:")
        if s == 'q':
            break   # 停止当前循环

    2. 流程控制 break和continue

    1. break:是直接跳出整个循环,终止执行.
    
    while True:
        s = input("请开始喷:")
        if s == 'q':
            break   # 停止当前循环
    2. continue: 结束本次循环,继续下一次的循环.
    
    while True:
        s = input("请开始喷:")
    # 过滤掉马化腾 if "马化腾" in s: print("你输入的内容和草泥马有一拼.不能输出") continue # 停止当前本次循环,继续执行下一次循环. print("喷的内容是:"+s)
    总结:
      (1) break跳出整个循环,而continue跳出本次循环
      (2) continue语句用来告诉python跳过当前循环,进行下一个循环

    注意:
      (1) 在嵌套循环中,break和continue只会对最近的一层循环起作用,也就是近原则.
      (2) break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。
      (3) break和continue语句用在while和for循环中.

    3. 格式化输出

    %s就是代表字符串占位符,除此之外,还有%d,是数字占位符.
    
    name = "alex"
    age = "21"
    hobby = ""
    gender = ""
    # 拼接很麻烦
    print(name+"今年"+age+"岁,是一个老头,爱好是"+hobby+",性别:"+gender)
    # %s: 字符串占位符
    print("%s今年%s岁,是一个老头,爱好是%s,性别:%s" % (name,age,hobby,gender))
    
    # %d: 数字占位符 age = 18 print("我今年%d岁了" % age) # 如果这个字符串中有了占位符,那么后面所有的%都是占位,需要转义. name = "alex" print("%s已经喜欢了沙河%%2的女生" % name) # 这⾥我们需要使⽤%%来表⽰字符串中的% print("wuse很色.喜欢了昌平%5的女生") #这句话中没有占位符.%还是%.

    4. 基本运算符

    1. 算数运算符
        +-*/%    取余
        //    取整
        **    幂
    2. 比较运算符
        ==    等于
        !=    不等于
        <>    不等于
         >    大于
         <    小于
        >=    大于等于
        <=    小于等于
    3.  赋值运算符
         =    是简单的赋值运算符 c = a + b,将a+b的运算结果赋值给c
        +=    是减法赋值运算符  c -= a等效于 c = c - a
        *=   是乘法赋值运算符  c *= a 等效于 c = c * a
        /=   是除法赋值运算符  c /= a 等效于 c = c / a
        %=   是取余赋值运算符  c %= a 等效于 c = c % a
        //=  是取整除赋值运算符 c //= a 等效于 c = c // a
        **=  是幂赋值运算符    c **= a 等效于 c = c ** a
    4. 逻辑运算符
      and    布尔""    a,b    a,b中有一个为 Falese, 那么a and b 为False.
      or     布尔""    a,b    a,b中有一个为 Ture, 那么a and b 为Ture.
      not    布尔""    a      a为 Falese, 那么 not a 为Ture.
    
      运算顺序: () > not > and > or
    
      # and or not同时存在. 先算括号,然后算not,然后算and,最后算or.
      print(3>4 or 4<3 and 1==1) # False
      print(1 < 2 and 3 < 4 or 1>2) # True
      print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # True
      print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # False
      print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # False
      print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # False
      数字的运算:
      x and y x为0,值为自己. x非0,值为y.
      x or y x为0,值为y. x非0,值为自己.
      x and y
      print(1 and 2)  #2
      print(2 and 0)  #0
      print(0 and 3)  #0
      print(0 and 4)  #0
    
      x or y 
      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
    
      注意: flase相当于0,但是不等于0.   
      print(2 > 5 and 3)  # Flase
      print(2 < 1 and 4 > 6 or 3 and 4 > 5 or 6)  # 6
  • 相关阅读:
    积水路面Wet Road Materials 2.3
    门控时钟问题
    饮料机问题
    Codeforces Round #340 (Div. 2) E. XOR and Favorite Number (莫队)
    Educational Codeforces Round 82 (Rated for Div. 2)部分题解
    Educational Codeforces Round 86 (Rated for Div. 2)部分题解
    Grakn Forces 2020部分题解
    2020 年百度之星·程序设计大赛
    POJ Nearest Common Ancestors (RMQ+树上dfs序求LCA)
    算法竞赛进阶指南 聚会 (LCA)
  • 原文地址:https://www.cnblogs.com/kangqi452/p/11258471.html
Copyright © 2011-2022 走看看