• day03_python基础


    一些会的内容就不做笔记了,做一些有点生疏的笔记

    1. 循环语句

    1.1 continue

    continue,在循环中用于 结束本次循环,开始下一次循环。

    示例1:

    print("开始")
    while True:
    	print("红旗飘飘,军号响。")
    	continue
    	print("剑已出鞘,雷鸣电闪。")
    	print("从来都是狭路相逢勇者胜。")
    print("结束")
    
    # 输出
    开始
    红旗飘飘,军号响。
    红旗飘飘,军号响。
    红旗飘飘,军号响。
    红旗飘飘,军号响。
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    示例2:

    print("开始")
    i = 1
    while i < 101:
    	if i == 7:
    		i = i + 1
    		continue
    	print(i)
    	i = i + 1
    print("结束")
    
    # 输出
    开始
    1
    2
    3
    4
    5
    6
    8
    9
    10
    ...
    100
    结束
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    示例3:

    print("开始")
    i = 1
    while True:
    	if i == 7:
    		i = i + 1
    		continue
    	print(i)
    	i = i + 1
    	if i == 101:
    		break
    print("结束")
    
    # 输出
    开始
    1
    2
    3
    4
    5
    6
    8
    9
    10
    ...
    100
    结束
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    写在最后,对于breakcontinue都是放在循环语句中用于控制循环过程的,一旦遇到break停止所有循环,一旦遇到continue就停止本次循环,开始下次循环

    当然,通过如果没有了break和continue,我们用while条件的判断以及其他协助也能完成很多功能,有了break和continue可以在一定程度上简化我们的代码逻辑。

    1.2 while…else

    当while后的条件不成立时,else中的代码就会执行。如果遇到break,则不会进入else

    #  书写格式
    while 条件:
      代码
      ...
    else:
      代码
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    while False:
        print('哈哈哈')
    else:
        print('else')
    #  结果为 else
    
    • 1
    • 2
    • 3
    • 4
    • 5
    n = 1
    while n < 3:
        print(n)
        n += 1
    else:
        print(666)
    # 运行结果
    1
    2
    666
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    有人会问,为什么不直接在while结束后直接写上print(666)

    n = 1
    while n < 3:
        print(n)
        n += 1
    print(666)
    #  结果也是和上面的一样的
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如果while循环中有break来终止循环,则会直接退出循环,并且不会进入到else

    n = 1
    while n < 3:
        print(n)
        break
    else:
        print(666)
    #  结果为 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2. 字符串格式化

    字符串格式化,使用更便捷的形式来实现字符串的拼接

    2.1 %

    2.1.1 基本格式化操作
    name = '王不易'
    #  占位符
    text = '我叫%s, 今年18岁' % '王不易'
    #  两种方法都是可以的
    text1 = '我叫%s, 今年18岁' % name
    print(text)
    print(text1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    多个占位符要用括号括起来(根据位置进行一一对应

    name = '王不易'
    age = 18
    #  占位符
    text = '我叫%s, 今年%s岁' % (name, age)
    print(text)
    #  结果: 我叫王不易, 今年18岁
    #  但是这样写不够严谨,因为整数可以用%d来表示,但上面这样写也是可以的
    text1 = '我叫%s, 今年%d岁' % (name, age)  # 结果与上面一样
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    下面是关于所有%的内容,不要求记住,不会的时候,点进去看一下就好了

    https://www.cnblogs.com/wupeiqi/articles/5484747.html

    下面根据名称对占位符进行一一对应

    #  %(name)s -> 给占位符加名称。后面要是字典的形式%{'name':'xxx'}
    name = '王不易'
    text1 = '%(name)s你快过来呀,今天桂千理不在家' % {'name':name}
    print(text1)
    
    #  多个占位符,则在中括号里加键值对就可以了
    name1 = '桂千理'
    text2 = '%(name)s你快过来呀,今天%(name1)s不在家' % {'name': name, 'name1': name1}
    print(text2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    2.1.2 百分比

    如果在一段字符串中有百分号存在,则需要写两个百分号,在字符串格式化的时候就知道这个不是占位符,只是普通的%,不写会报错

    name = '王不易'
    s = '%s,我的片以及下载了90%了,居然断网了' % s
    print(s)  # 这样子输出会报错
    
    #  正确的写法
    s = '%s,我的片以及下载了90%%了,居然断网了' % s
    print(s)  # 王不易,我的片以及下载了90%了,居然断网了
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    总结:一旦字符串格式化中存在百分比的显示,请一定要加%%以实现输出%。

    2.2 format

    name = '王不易'
    age = 18
    
    #  和上面一样,按照位置一一对应
    text = '我叫{},今年18岁'.format(name)  # {} 占位符
    print(text)  # 我叫王不易,今年18岁
    
    text = '我叫{},今年{}岁'.format(name, age)  # 传多个
    print(text)  # 我叫王不易,今年18岁
    
    text = '我叫{},今年{}岁,真实的名字也叫{}'.format(name, age, name)  # 传多个
    print(text)  # 我叫王不易,今年18岁,真实的名字也叫王不易
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    name = '王不易'
    age = 18
    #  带数字的一一对应(索引)
    text = '我叫{0},今年18岁'.format(name)  # {0} 占位符
    print(text)  # 我叫王不易,今年18岁
    
    #  类似列表,表示将format中的第几个索引传过去
    text = '我叫{0},今年{1}岁'.format(name, age)  # 传多个
    print(text)  # 我叫王不易,今年18岁
    
    #  能够复用
    text = '我叫{0},今年{1}岁,真实的名字也叫{0}'.format(name, age)  # 这样穿参数就比上面方便很多
    print(text)  # 我叫王不易,今年18岁,真实的名字也叫王不易
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    #  通过名称一一对应
    name = '王不易'
    age = 18
    text = '我叫{n1},今年18岁'.format(n1=name)
    print(text)  # 我叫王不易,今年18岁
    
    text = '我叫{n1},今年{n2}岁'.format(n1=name, n2=age)  # 传多个  # 也是可以复用的
    print(text)  # 我叫王不易,今年18岁
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    #  当复用模版
    text = '我叫{0},今年{1}岁'  # 当成一个模版,可以多次使用
    
    data1 = text.format('王不易', 18)
    data2 = text.format('桂千理', 20)
    print(data1)  # 我叫王不易,今年18岁
    print(data2)  # 我叫桂千理,今年20岁
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    #  % 也可以当成模版来使用
    text = '我叫%s, 今年%d岁'
    data1 = text %('王不易', 18)
    data2 = text %('桂千理', 20)
    print(data1)  # 我叫王不易,今年18岁
    print(data2)  # 我叫桂千理,今年20岁
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.3 f

    比前两种方便

    #  通过名称一一对应
    name = '王不易'
    age = 18
    text = f'我叫{name},今年{age}岁'
    print(text)  # 我叫王不易,今年18岁
    
    • 1
    • 2
    • 3
    • 4
    • 5
    #  在括号里面做算术也是可以的
    text = f'我叫{name},今年{2+16}岁'
    print(text)  # 我叫王不易,今年18岁
    
    • 1
    • 2
    • 3
    # 在上面的基础上加等于,就可以相当于整个算术
    text = f'我叫{name},今年{2+16=}岁'
    print(text)  # 我叫王不易,今年2+16=18岁
    
    • 1
    • 2
    • 3
    #  进制转换
    v1 = f'嫂子今年{22}岁'
    print(v1)  # 嫂子今年22岁
    
    #  将22转换为二进制
    v2 = f'嫂子今年{22: #b}岁'
    print(v2)  # 嫂子今年 0b10110岁
    
    #  将22转换为八进制
    v3 = f'嫂子今年{22: #o}岁'
    print(v3)  # 嫂子今年 0o26岁
    
    #  将22转换为十六进制
    v4 = f'嫂子今年{22: #x}岁'
    print(v4)  # 嫂子今年 0x16岁
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    #  理解 
    #  f的花括号里面还可以传入方法/函数
    name = 'amaisi'
    text = f'我叫{name},今年18岁。'
    print(text)  # 得到的就是 我叫amaisi,今年18岁。
    #  如果我想将name改为大写,则可以这样写(直接传入方法)
    text = f'我叫{name.upper()},今年18岁。'
    print(text)  # 我叫AMAISI,今年18岁。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3. 运算符

    3.1 面试题

    逻辑运算中: and or

    name = 'alex'
    pwd = '123'
    v1 = name == 'alex' and pwd == '123'
    print(v1)  # True
    
    name = 'alex'
    pwd = '3'
    v1 = name == 'alex' and pwd == '123'
    print(v1)  # False
    
    #  上面类似于
    if name == 'alex' and pwd == '123'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    #  面试题2
    v2 = 'wmj' and 'gql'
    #  问: v2返回什么?
    print(v2)  # gql
    
    '''
    解:
    	第一步: 将and前后的值转换为布尔值 -> True and True
    	第二步: 判断本次操作取决于谁? 如果前面是False,则无需看后一个值,由于前面是True,需要去查看后面的值是什么,所以本次逻辑判断取决于后面的值。
    	所以,后面的值等于多少,最终的结果就是多少。 所以v2 = 'gql'
    '''
    
    v3 = '' and 'wby'  # False and True
    #  所以直接返回空
    print(v3)  # 返回空
    
    v4 = 1 or 8
    #  第一步: 将or前后的值转换为布尔值 -> True and True
    #  第二步: 判断本次操作取决于谁? 由于前面是True,所以本次逻辑判断取决于前面的值。
    print(v4)  # 1
    
    v5 = 0 or 8
    #  第一步: 将or前后的值转换为布尔值 -> False and True
    #  第二步: 判断本次操作取决于谁? 由于前面是False,所以本次逻辑判断取决于后面的值。
    print(v5)  # 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3.2 练习题

    v1 = 1 or 2
    v2 = -1 or 3
    v3 = 0 or -1
    v4 = 0 or 100
    v5 = "" or 10
    v6 = "wupeiqi" or ""
    v7 = 0 or ""
    
    print(v1,v2,v3,v4,v5,v6,v7)
    #  结果: 
    #  v1 = 1
    #  v2 = -1
    #  v3 = -1
    #  v4 = 100
    #  v5 = 10
    #  v6 = "wupeiqi"
    #  v7 = ''
    
    #  总结: or, 看第一个值,如果第一个值为真,结果就应该是第一个值,否则结果就是第二个值。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    v1 = 4 and 8
    v2 = 0 and 6
    v3 = -1 and 88
    v4 = "" and 7
    v5 = "武沛齐" and ""
    v6 = "" and 0
    v7 = 0 and "中国"
    
    print(v1,v2,v3,v4,v5,v6,v7)
    #  结果: 
    #  v1 = 8
    #  v2 = 0
    #  v3 = 88
    #  v4 = ''
    #  v5 = ''
    #  v6 = ''
    #  v7 = ''
    #  总结: and, 看第一个值,如果第一个值为真,结果就应该是第二个值,否则就是第一个值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.3 究极面试题(or 和 and 结合 )

    思路: 如果多个and和or的情况,则先计算and再计算or

    v1 = 0 or 4 and 3 or 7 or 9 and 6
    	解:	 0 or 3 or 7 or 9 and 6
      	 0 or 3 or 7 or 6	
         3 or 7 or 6
         3 or 6
         3
    v2 = 8 or 3 and 4 or 2 and 0 or 9 and 7
    		 8 or 4 or 2 and 0 or 9 and 7
      	 8 or 4 or 0 or 9 and 7
         8 or 4 or 0 or 7
         8 or 0 or 7
         8 or 7
         8
    v3 = 0 or 2 and 3 and 4 or 6 and 0 or 3
    		 0 or 3 and 4 or 6 and 0 or 3
    		 0 or 4 or 6 and 0 or 3
      	 0 or 4 or 0 or 3
         4 or 0 or 3
         4 or 3
         4
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    补充: 既有not,又有and,还有 or

    思路: 先计算not,再计算and,最后计算or

    v4 = not 8 or 3 and 4 or 2
    		 False or 3 and 4 or 2
      	 3 and 4 or 2
         4 or 2
         4
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    今日练习

    先判断(大于、小于),再判断(not 、and 、or)

    1. 判断下列逻辑语句的True,False

      1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
      		False or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  # 先处理大于、小于
          False or True or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
          False or True or False and 2 > 1 and 9 > 8 or 7 < 6
          False or True or False and True and 9 > 8 or 7 < 6
          False or True or False and True and True or 7 < 6
          False or True or False and True and True or False  # 先判断and
          False or True or True and True or False
          False or True or True or False
          True or True or False
          True or False
          True
      not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
      		not True and True or False and True and True or False  #  先处理大于、小于
        	False and True or False and True and True or False  # 处理not
          False  # 处理and
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    2. 求出下列逻辑语句的值。

      8 or 3 and 4 or 2 and 0 or 9 and 7
      		8 or 4 or 0 or 7  # 先判断and
          8  # 处理not
      0 or 2 and 3 and 4 or 6 and 0 or 3
      		0 or 4 or 0 or 3  # 先判断and
          4 # 处理not
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. 下列结果是什么?

      6 or 2 > 1  # 6
      3 or 2 > 1  # 3
      0 or 5 < 4  # False
      5 < 4 or 3  # 3
      2 > 1 or 6  # True
      3 and 2 > 1  # True
      0 and 3 > 1  # True   -> 这题的答案是 0
      	0 and True
        0
      2 > 1 and 3  # 3
      3 > 1 and 0  # 0
      3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2  
      		True and 2 or True and 3 and 4 or True
        	2 or 4 or True
          2
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
    4. 实现用户登录系统,并且要支持连续三次输错之后直接退出,并且在每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)。

      print('欢迎来到德莱联盟')
      times = 3  # 用于控制次数
      while True:
          name = input('请输入你的用户名: ')
          pwd = input('请输入你的密码: ')
          if name == '王不易' and pwd == '123':
              print('欢迎回家')
              break
          times -= 1
          if times == 0:
              print('你的次数已用完,即将关闭程序')
              break
          print(f'密码或用户名输入有误,剩余错误次数{times}')
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    5. 猜年龄游戏
      要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出。

      print('来猜猜我的年龄吧~~~~')
      times = 3  # 用于控制次数
      age = 18
      while True:
          number = input('请大胆输入我的年龄: ')
          number = int(number)
          if number == age:
              print('恭喜你,答对了!!!')
              break
          times -= 1
          if times == 0:
              print('你的次数已用完,你太让我失望了')
              break
          print(f'不对不对,还有{times}次机会')
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    6. 猜年龄游戏升级版
      要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出。

    print('来猜猜我的年龄吧~~~~')
    times = 3  # 用于控制次数
    age = 18
    while True:
        number = input('请大胆输入我的年龄: ')
        number = int(number)
        if number == age:
            print('恭喜你,答对了!!!')
            break
        times -= 1
        if times == 0:
            print('你的次数已用完,你太让我失望了')
            n = input('再给你一次机会,你还玩不玩,回答Y,就再给你猜三次,回答N,就拜拜 : ')
            if n == 'Y':
                times = 3
            else:
                break
        print(f'不对不对,还有{times}次机会')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    [安卓逆向]apktool实现APK反编译、重打包、签名
    记一次 .NET某企业数字化平台 崩溃分析
    使用 Docker 安装 IPFS 星际文件系统
    Python安装pycrypto出错处理方法
    Java零基础入门-多维数组
    Mysql join用法详解
    目标检测YOLO实战应用案例100讲-基于YOLOv5的航拍图像旋转目标检测(下)
    SCI写作指南
    【Java第35期】:Bean的生命周期
    RepVgg实战:使用RepVgg实现图像分类(一)
  • 原文地址:https://blog.csdn.net/m0_48936146/article/details/127820381