Day02 python基础知识

day2 内容大纲

  1. while 循环

    • why:大气循环, 吃饭,上课,睡觉,日复一日,歌曲列表循序环,程序中:输入用户名密码,

    • what:while 无限循环。

    • how:

      1. 基本结构:

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

        while True:
            print('狼的诱惑')
            print('我们不一样')
            print('月亮之上')
            print('庐州月')
            print('人间')
            
        
      3. 基本原理:

      4. 循环如何终止?

        1. 改变条件。

          flag = True
          while flag:
              print('狼的诱惑')
              print('我们不一样')
              print('月亮之上')
              flag = False
              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)
          
          

        1. break
        # while True:
        #     print('狼的诱惑')
        #     print('我们不一样')
        #     print('月亮之上')
        #     break
        #     print('庐州月')
        #     print('人间')
        # print(111)
        
        1. 系统命令(今天不讲)

        2. continue

          # continue : 退出本次循环,继续下一次循环
          flag = True
          while flag:
              print(111)
              print(222)
              flag = False
              continue
              print(333)
          
          
          # while else: while 循环如果被break打断,则不执行else语句。
          count = 1
          while count < 5:
              print(count)
              if count == 2:
                  break
              count = count + 1
          else:
              print(666)
          
    • where: 你需要重复之前的动作,输入用户名密码,考虑到while循环。

  2. 格式化输出

    • 当你遇到这样的需求:字符串中想让某些位置变成动态可传入的,首先要考虑到格式化输出。
  3. 运算符:算数运算符 + -,比较运算符 > ==,赋值运算符=,+=,逻辑运算符,and or, 成员运算符。

    i1 = 2
    i2 = 3
    print(2 ** 3)
    print(10 // 3)
    print(10 % 3)
    
    print(3 != 4)
    
    count = 1
    count = count + 1
    count += 1
    print(count)
    
    
    # and or not
    
    # 1 在没有()的情况下,优先级:not > and > or,同一优先级从左至右依次计算
    # 情况1:两边都是比较运算
    # print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
    # print(True or False)
    
    # 情况2:两边都是整数
    '''
    x or y , x为真,值就是x,x为假,值是y
    '''
    # 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
    
    
  4. 编码的初识重点

​ ASCII码:只包含:英文字母,数字,特殊字符。

​ 0000 0001 : a

​ 0000 0101 : ;

​ 8bit == 1byte

​ 'hello123': 8byte

​ gbk: 英文字母,数字,特殊字符和中文。国标

​ 一个英文字母: 0000 0001 : a

​ 一个中文 中: 0000 0001 0100 0001 : 中

​ Unicode: 万国码:把世界上所有的文字都记录到这个密码本。

​ 起初一个字符用2个字节表示:

​ 0000 0001 0000 0011: a

​ 0000 0001 0100 0001 : 中

​ 后来为了涵盖全部文字:

​ 0000 0001 0000 0011 0000 0001 0000 0011: a

​ 0000 0001 0100 0001 0000 0001 0000 0011 : 中

​ 浪费空间,浪费资源。

​ Utf-8:升级:最少用8bit1个字节表示一个字符。

​ 0000 0011: a 1字节

​ 0000 0011 0000 0011 欧洲 2个字节

​ 0000 0011 0000 0011 0000 0011 中: 3个字节。

重点:

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

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

8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024TB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB    

# 1.判断下列逻辑语句的True,False.
# 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# F or T or T and T and T or F
# print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
# T
# 2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# F and T or F and T and T or F
# print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
# F
# 2.求出下列逻辑语句的值。
# 1),8 or 3 and 4 or 2 and 0 or 9 and 7
# print(8 or 3 and 4 or 2 and 0 or 9 and 7)
# 8
# 2),0 or 2 and 3 and 4 or 6 and 0 or 3
# print(0 or 2 and 3 and 4 or 6 and 0 or 3)
# 4
# 3.下列结果是什么?
# 1)、6 or 2 > 1  6
# 2)、3 or 2 > 1  3
# 3)、0 or 5 < 4  F
# 4)、5 < 4 or 3  3
# 5)、2 > 1 or 6  T
# 6)、3 and 2 > 1  T
# 7)、0 and 3 > 1  0
# 8)、2 > 1 and 3  3
# 9)、3 > 1 and 0  0
# 10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2  2
# 4.while循环语句基本结构?
# while 条件:
#     循环体
# 6.利用while语句写出猜大小的游戏:
# 设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。
# while 1:
#     num = int(input("请输入数字:"))
#     if num > 66:
#         print("猜大了")
#     elif num < 66:
#         print("猜小了")
#     else:
#         print("结果正确")
#         break
# 7:在5题的基础上进行升级:
# 给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘太笨了你....’。
# count = 0
# while count < 3:
#     num = int(input("请输入数字:"))
#     if num > 66:
#         print("猜大了")
#     elif num < 66:
#         print("猜小了")
#     else:
#         print("结果正确")
#         break
#     count += 1
# else:
#     print("太笨了!")
# 8.使用while循环输出 1 2 3 4 5 6 8 9 10
# count = 1
# while count < 11:
#     print(count)
#     count += 1
# 9.求1-100的所有数的和
# count = 1
# s = 0 
# while count < 101:
#     s += count
#     count += 1
# print(s)

# 10.输出 1-100 内的所有奇数
# count = 1
# while count < 101:
#     if count%2 == 1:
#         print(count)
#     count += 1

# 11.输出 1-100 内的所有偶数
# count = 1
# while count < 101:
#     if count%2 == 0:
#         print(count)
#     count += 1
# 12.求1-2+3-4+5 ... 99的所有数的和
# count = 1
# s = 0 
# while count < 101:
#     if count%2 == 1:
#         s += count
#     else:
#         s -= count
#     count += 1
    
# # print(s)
# # 13.用户登录(三次输错机会)且每次输错误时显示剩余错误次数(提示:使用字符串格式化)
# count = 0
# while count < 3:
#     username = input("请输入账号:")
#     password = input("请输入密码:")
#     if username == "woni" and password == "woni123":
#         print("登陆成功!")
#         break
#     else:
#         print("账号或密码错误,你还有%d次机会"%(2-count))
#     count += 1
# 14.简述ASCII、Unicode、utf-8编码
# ASCII最开始的一个ASCII码对应一个字节码
# unicode 4个字节
# gbk 英文1个字节,中文2个字节
# utf-8 英文一个字节,欧洲字2个字节 中文三个字节
# 15简述位和字节的关系?
# 8bit = 1Byte
posted @ 2020-04-22 23:18  Woni  阅读(116)  评论(0)    收藏  举报