while循环,格式化输出%,运算符,数据类型的转换,编码的初识,

  1. while 循环

    • where:程序中:你需要重复之前的动作,输入用户名密码时,考虑到while循环。

    • what:while 无限循环。

    • how:

      1. 基本结构:

        while 条件:
            循环体
      2. 初识循环

        while True:
            print('狼的诱惑')
            print('我们不一样')
            print('月亮之上')
            print('庐州月')
            print('人间')
        
      3. 循环如何终止?--3种

        1. 改变条件。

           flag = True     #flag标志位
           while flag:
              print('狼的诱惑')
              print('我们不一样')
              print('月亮之上')
              flag = False   #下面的会执行.但只会循环1次
              print('庐州月')
              print('人间')
          
          # 练习: 打印1~ 100 所有的数字
          count = 1    #法一 标志位
          flag = True
          while flag:
              print(count)
              count = count + 1
              if count == 101:
                  flag = False
          
          count = 1   #法二    
          while count < 101:    #看好是先打印还是先自加
              print(count)
              count = count + 1
          
          # 打印1 + 2 + 3 + ...... 100  的最终结果:
          
          s = 0
          count = 1
          while count < 101:
              s = s + count
              count = count + 1
          print(s)
          
      4. break 跳出循环

        # while True:
        #     print('狼的诱惑')
        #     print('我们不一样')
        #     print('月亮之上')
        #     break
        #     print('庐州月')
        #     print('人间')
        #     print(111)

        补充几个例子:

        #正常例子:
        while True:
            print(123)
            break
            print(666) #不打印,因为while被break中止,循环体里break下面的程序不再执行.
        #输出结果:123
        #判断是否会打印与while平行的程序:
        print(456)
        while True:
            print(111)
            print(222)
        print(666)  #不执行,因为它与while平行,需执行完while再执行它;但此时的while是无限(死)循环
        #判断是否会打印与while平行的程序:
        while True:
            print(111)
            print(222)
            break
        print(666)  #执行此程序,因为循环体已被break中止,故死循环-->有限循环,所以while循环下面的程序会执行
        
        #输出结果:
        111
        222
        666
        
      5. continue

        # continue : 退出本次循环,继续下一次循环 #相当于回到了循环体的底部
        flag = True
        while flag:
            print(111)
            print(222)
            flag = False
            continue
            print(333)  #不执行  #continue下面的程序不再执行
        #若没有flag = False,则会一直打印111,222
        
      6. 系统命令(后续)

    4.while else

while else: while 循环如果被break打断,则不执行else语句。

count = 1
while count < 5:
print(count)
if count == 2:
break
count = count + 1
else:
print(666)

   
2. 格式化输出

   + 当你遇到这样的需求:字符串中想让某些位置变成动态可传入的,首先要考虑到格式化输出。

     例如: 制作一个公共的个人信息模板

 name = input('请输入你的姓名:')
 age = input('请输入你的年龄:')
 job = input('请输入你的工作:')
 hobby = input('请输入你的爱好:')
 msg = '''------------ info of %s  -----------     # 这只是简历的一种格式,info-信息
 Name  : %s                                        # % 叫占位符,str是%s, int是%d
 Age   : %d
 job   : %s
 Hobbie: %s
 ------------- end -----------------''' % (name, name, int(age), job, hobby)  
 print(msg)     #共有5个位置,要一一对应。

- 坑:在格式化输出中,若% 只想表示单纯的一个百分号,而不是作为占位符使用,则应在%后再加一个%!!!

msg = '我叫%s,今年%s,学习进度1%%' % ('太白金星', 18) #注意str引号的位置
print(msg)

```

  1. 运算符:算数运算符 +-*/%**//,比较运算符 < > <>或!= == >= <=,赋值运算符=,+=,-=,逻辑运算符and or not(与或非)。 #见教材P33.

    i1 = 2
    i2 = 3
    print(2 ** 3)  #n次方   #8    #注:不是平方,是次方!
    print(10 // 3)  #取整   #3
    print(10 % 3)  #取余   #1
    
    print(3 != 4)  #不等于
    
    count = 1
    count = count + 1
    count += 1    #等同count = count + 1
    print(count)
    
    
    # and or not
    
    # 1 在没有()的情况下,优先级:not > and > or,同一优先级从左至右依次计算
    # 情况1:两边都是比较运算
    # print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)      #and两边都true才是true
    #=ptint(True and True) or (False and False)     #or只要一边为true即true
    #=print(True or False)
    #=print(True)
    # 情况2:两边都是整数
    '''
    x or y , x为真,值就是x,x为假,值是y   #or,左为真返回左,否则返回右.and的返回值与之相反.
    '''
    # print(1 or 2)
    # print(3 or 2)
    # print(4 or 2)
    # print(-1 or 2)
    # print(0 or 2)
    
    # print(1 and 2)
    
    

    数据类型之间的转换

    # str ---> int  : 只能是纯数字组成的字符串
    s1 = '00100'
    print(int(s1))
    # int ----> str
    i1 = 100
    print(str(i1),type(str(i1)))
    
    # int  ---> bool  : 非零即True ,0为False。
    i = 0
    print(bool(i))
    # bool ---> int
    print(int(True))  # 1
    print(int(False))  # 0
    
    
  2. 编码的初识

计算机存储文件,存储数据,以及将一些数据信息通过网络发送出去,存储发送数据什么内容?---底层都是01010101.

编码(密码本):指二进制文字之间的对应关系

重点:

(1).知道4种编码各占多少字节

  • 总览:

ASCII:只包含 英文字母,数字,特殊字符。均占8位(1字节) #不支持中文

Unicode:万国码,包括世界上所有的文字,中文,英文,符号,均占32位(4字节)。浪费空间,浪费资源。

gbk: 国标, 包含英文字母,数字,特殊字符和中文。英文 1个字节 中文 2个字节

utf-8:万国码升级版,英文占8位(1字节)、欧洲字符占16位(2字节)、中文字符站24位(3字节)

  • 详情:

ASCII码(最早的密码本):只包含英文字母,数字,特殊字符(记录的是二进制与英文字母,特殊符号以及数字的对应关系)。

英文字母,数字,特殊字符均占1个字节(8位)

0000 0001 : a

0000 0101 : ;

8bit(位) == 1byte(字节)

eg: 'hello12': 8byte #字符串"hello12"共占7个字节.

'abc' : 占3个字节 01100001 01100010 01100011

gbk(国标)*: 包含英文字母,数字,特殊字符和中文。

英文 1个字节 中文 2个字节

eg: 'ab小黑' :6个字节

Unicode:万国码,包括世界上所有的文字,不论中文、英文或符号,每个字符均占32位(4字节)。浪费空间,浪费资源。

优点: 兼容性高,他与任何的密码本都有映射关系

缺点; 浪费空间,浪费资源。

Utf-8(最常用):万国码升级版,英文占8位(1字节)、欧洲字符占16位(2字节)、中文字符站24位(3字节)

​ 0000 0011: a 1字节 沿用的ASCII码

​ 0000 0011 0000 0011 欧洲 2个字节

0000 0011 0000 0011 0000 0011 中 3个字节

eg: 'ab小黑' 8个字节

eg: '中国12he' : GBK: 8个字节

'中国12he' : UTF-8: 10个字节

(2).单位是咋换算

8bit = 1byte  #最小的单位是bit
1024byte = 1KB  #这些...B里最小的是kB,最大的是DB,对应关系均为1024
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB #此处
1024TB = 1PB
1024TB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB    

eg:计算7.6MB=多少bit?

7.6MB ----> 7.6 * 1024 * 1024 * 8

猜你喜欢

转载自www.cnblogs.com/wanshizidiao/p/10989123.html