while循环及运算符

第二天笔记整理

while 循环

  • 定义:不断地重复着某件事情就是循环

  • while是一个关键字

  • while 语法

  • while 条件:
          循环体
    
  • while循环中的两个关键字 break 、continue

    • break:终止当前循环

    • 举例说明:

    • #本次循环循环5次,不会再循环第6次
      count = 0
      while True:            # 死循环
          count = count + 1  # 先执行 = 右边的内容
          if count == 5:
              print(count)
              break          # 有限循环
      
    • continue:跳出当前循环,继续下次循环(就是伪装成循环体中的最后一行代码)

    • 举例说明:

    • #本次循环是一个死循环,只是循环期间的第5次结果不会出现,之后接着死循环
      count = 0
      while True:            # 死循环
          count = count + 1  # 先执行 = 右边的内容
          if count == 5:
             print(111)
             continue        # continue 就是伪装成循环体中最后一行代码
             print(count)
      
    • 达到循环体的条件时,break 和 continue 下方的代码都不会执行

    • 举例说明:

    • #本次循环体中的continue没有用,可以去掉
      count = 0
      while True:             # 死循环
         count = count + 1    # 先执行 = 右边的内容
         continue             # continue 就是伪装成循环体中最后一行代码
         print(count)        
      
  • 通过条件控制循环次数

    • 举例说明:

    • 题目:输出4~67,包含4和67以及之间的数字
      答案代码:
      count = 4             #计算器
      while count <= 67:
          print(count)
          count += 1        #计算器进行计算
      
    •  书写错误举例说明:

    • #本次循环是一个死循环,因为本次循环没有计算器(书写是记得添加计算器)
      print(666)
      count = 0
      while count < 3:
         print(count)   # 一直死循环,计算器没有进行计算
      
  • while else (与上节课讲的 if else相似)

    • 举例说明:

    • 题目:输出666 0 1 2 888
      答案代码:
      print(222)
      count = 0
      while count < 3:
          print(count)
          count = count + 1
      else:
          print(888)
      
    •  再举个例子说明:

    • 题目:输出222 0
      答案代码:               
      #思路:0<3,符合条件,进入循环体后,通过print输出0,之后输入到计算器变成1,碰到break终止当前循环,包括else不再执行,(while 和 else 是一整个循环)
      print(222)
      count = 0
      while count < 3:
          print(count)
          count = count + 1
          break
      else:
          print(111)
      
    • 举个例子证明break跳出当前循环:

    • 题目:输出220 0 1 2 111
      答案代码:
      #说明一下:这个代码比上述例子少了break关键字,导致结果就不一样!
      #思路:先输出222,0<3,符合条件进入循环体,输出0,通过计算器变成1,1<3再次进入循环体,输出1,通过计算器变成2,2<3再次进入循环体,输出2,通过计算器变成3,3=3不符合条件,进入else,输出111
      print(222)
      count = 0
      while count < 3:
          print(count)
          count = count + 1
      else:
          print(111)
      

格式化

  • %s 字符串 => %s可以填充字符串也可以填充数字

  • %d 和 %i 整型 => 必须填充数字

  • %%  转义 => 变成普通的%

  • 按照位置顺序一一对应(占了几个位置就填几个位置)

  • f-strings python3.6版本及以上才能使用

  • 代码复杂化举例

  • name = input("name")
    age = input("age")
    sex = input("sex")
    hobby = input("hobby")
    msg = """
    ------info------
    name:%s
    age:%s
    sex:%s
    hobby:%s
    -------end------
    """
    print(msg%(name,int(age),sex,hobby))
    
  • 代码简单化举例

  • msg = "my name is %s I'm %s years old"        # 单双引号配合使用
    print(msg%(input("name:"),input("age:")))
    
  • 代码更简单化举例(f)

  • msg = f"my name % {{}} is {input('name:')} I'm {input('age:')} years old"
    print(msg)
    

运算符

  • 比较运算符

  • >  <  >=  <=  ==(等于) != (不等于)
    
  • 算数运算符

  • + 加 
    - 减
    * 乘
    / 除
    // 整除|地板除(向下取整)
    ** 幂
    % 取余(模)
    
  • 赋值运算符

  • =  赋值
    +=  加等于
    -=  减等于
    *=  乘等于
    /=  除等于
    //= 整除等于
    **= 幂等于
    %=  模等于
    
  • 逻辑运算符

  • 与(and 并且) 或(or) 非(not 不是)
    True and False
    True or False
    True and not False
    优先级:() > not > and > or 
    查找顺序: 从左向右
    比如:print(8 and True)   => 
    
    • True和False进行逻辑运算时:
          and:
              一真一假,就是假
              同真为真,同假为假
      
          or:
              一真一假,就是真
              同真为真,同假为假
      
    •  面试题

    • and数字进行逻辑运算时:
          数字不为 0 时和不为 False
                and运算选择and后边的内容
                and运算都为假时选择and前的内容
                and 运算一真一假选择假
      
      or 数字进行逻辑运算时:
          数字不为 0 时和不为 False
                or运算选择or前边的内容
                or运算都为假时选择or后边的内容
                or 运算一真一假选择真
      
  • 成员运算法

  • in(在)    not in(不在)
    
  • 举例:

  • name = "alex"
    msg = input(">>>")
    if name in msg:
        print(111)
    else:
        print(222)
    

编码初识

  • 编码集(密码本)

    • ascii
           定义:将英文字母和常用符号用特定的数字去表达,但不支持中文
           比如:a 一个字符占用8位
      gbk(包含ascii)国标
            a 一个字符占用8位(1字节)
           中文 一个字符占16位(2字节)
      unicode
           定义:包含所有字符的字符集,解决了世界各地每个地方所用的编码方式不一样的尴尬,让我们更好沟通。
           比如:英文  4个字节  32位
                中文  4个字节  32位
      utf-8 (最流行的编码集)
           定义:为了解决Unicode存在内存问题,将Unicode再次编码的一种编码方式
           比如:英文 1字节      8位
                欧洲 2字节      16位
                亚洲 3字节      24位
      
  • 单位转换:

  • 8bit = 1byte
    1024byte = 1KB
    1024KB = 1MB
    1024MB = 1GB
    1024GB = 1TB
    1024TB = 1PB
    1024TB = 1EB
    1024EB = 1ZB
    1024ZB = 1YB
    1024YB = 1NB
    1024NB = 1DB
    常⽤到TB就够了
    
posted @ 2019-09-06 20:55  小皮浩  阅读(331)  评论(0编辑  收藏  举报