while循环以及运算符

1.循环语句

while 基本框架

while 后接条件语句:

缩进后代码

  • break 跳出当前循环

  • continue 本次循环遇到continue,则不再往下执行,返回到while循环条件判断

  • while... ... else 当不再满足while条件时,触发执行else,或者条件为False时执行

#6. 请通过循环,1 2 3 4 5 6 8 9 10.
count = 1
while count <= 10:
   if count != 7:
       print(count)
   count += 1

 

2.字符串格式化

%s 占位符

%d数字占位符

%% 格式化中充当百分号

3.运算符

  1. 算术运算符

    #练习  1~100 所有数字相加
    count = 1
    sum = 0
    while count <= 100:
       sum = sum + count
       count += 1
    print(sum)

  2. 赋值运算符

    count = 1
    while count <= 100:
       print(count)
       count +=1 #count = count + 1

     

  3. 逻辑运算符

    • 一般用作判断

      if 1 > 0 and 2 > 3:

      print('666')
    • 二般情况,用作取值

      • or

      对于or,如果遇到 value = 1 or 9

      第一个值入股转换成布尔值结果为真,则取第一个值。

      第一个值如果转换成布尔值结果为假,则去第二个值。

      如果有多个or条件,从左到右依次执行上述流程

      #实例
      v1 = 0 or 1   #结果0
      v2 = 6 or 9   #结果9
      v3 = 0 or 9 or 8   #结果9
      • and

        对于and,如果遇到 value = 1 or 9 这种情况

        第一个值如果转换成布尔值结果为真,则取第二个值。

        第一个值如果抓换成布尔值结果为假,则去第一个值

        如果有多个and条件,从左到右依次执行上述条件

        #实例
        #v1 = 1 and 9 #结果为9
        #v2 = 1 and 0 #结果为1
        #v3 = 0 and 7 #结果为0
        #v4 = 0 and '' #结果为0
        v5 = 1 and 0 and 9 #结果为0
        print(v5)
      • 其他

        • 优先级 not优先级高于and ,and优先级高于or。即()>not>and>or,同意优先级从左往右依次运算

        • 数据类型转换

      4.运算符补充

    • in

    value = "我是中国人"
    #判断‘中国是否在value所代指的字符串中’,“中国”是否在value所代指的字符串的子序列中。
    v1 = "中国" in value
    #示例
    while True:
       count = input("欢迎来到装逼联盟")
       shield = "装逼"
       if shield in count:
           print("装逼如风,常伴吾身,你不行")
           continue
       else:
           print(count)
           break
  • not in

    1. 优先级

      not 2 > 1

      not 2     >    1   # 错误
      not    2>1   # 正确

       

4.编码

  • 编码扩展

    • ascii

    • unicode

      • ecs2

      • ecs4

    • utf=8,中文用3个字节

    • utf-16

    • gbk 中文用2个字节

    • gb2315 中文用3个字节

  • 单位

     

  •  

猜你喜欢

转载自www.cnblogs.com/jhui104/p/10630583.html