zoukankan      html  css  js  c++  java
  • python基本操作(五)

    if 判断

    if 条件:
    代码1
    代码2
    代码3
    代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行)
    cls ='humale'
    gender = 'female'
    age = 18
    if cls =='human' and gender =='female' and age >16 and age < 22:
    print('开始表白')
    print('end...')

    语法

    • if
    • if...else
    • if...elif...else

    elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么。

    cls = 'human'
    gender = 'female'
    age = 28
    if cls == 'human' and gender == 'female' and age > 16 and age < 22:
    print('开始表白')
    elif cls == 'human' and gender == 'female' and age > 22 and age < 30:
    print('考虑下')
    else:
    print('阿姨好')

    while循环

    while 条件为 true

    ​ 代码块

    while 1:
    egon_age = 73

    age = input('请输入你猜的年龄》》》')
    age = int(age)
    
    if age == egon_age:
        print('猜对了')
    elif age > egon_age:
        print('猜大了')
    elif age < egon_age:
        print('猜小了')
    

    print('我跳出while循环了')

    • while+break

    while 1:
    egon_age = 73

    age = input('请输入你猜的年龄》》》')  # 73
    if age == egon_age:  # 成立
        print('猜对了')
        break
    elif age > egon_age:
        print('猜大了')
    elif age < egon_age:
        print('猜小了')
    
    • while循环嵌套

    prize = {0: 'zhangyan', 1: 'bu_wawa', 2: 'nick', 3: 'balloon_wawa'}

    while 1:
    egon_age = 73

    age = input('请输入你猜的年龄》》》')  # 73
    age = int(age)  # 73
    
    if age == egon_age:  # 成立
        while True:
            print(F'猜对了!!!
    请选择下列奖项中的一个:{prize}')
            choice = input('请选择你需要的奖品。')
            choice = int(choice)
            if choice == 2:
                print('大傻,这个不能给你')
                print('重新选择
    ')
            else:
                print(f'大傻,{prize[choice]}这个你也要
    ')
                break
        break
    elif age > egon_age:
        print('猜大了')
    elif age < egon_age:
        print('猜小了')
    
    • while+continue

    count = 1
    while count < 101: # 1<101,2<101,count=50

    if count == 50:
        count += 1
        continue   # 不执行下面的代码
    
    print(count)  # 1,2
    count += 1  # count=2,count=3
    

    break直接终止整个while循环;continue只是不执行下面的代码,但是会继续循环下去

    • while+elsea

      当while循环没有被break掉的时候,会执行else下面的代码。while 不能为true

      count = 1
      while count < 101: # 1<101,2<101,count=50,
      if count == 50:
      count += 1
      break # 不执行下面的代码

      print(count)  # 1,2
      count += 1  # count=2,count=3
      

      else:
      print('我没有被break掉')

    for循环

    for循环的循环次数受限于容器类型的长度,而while循环的循环次数需要自己控制。for循环也可以按照索引取值。
    

    ​ for i in 列表和字典

    ​ i就是列表的每个元素

    • for+break

    ​ for循环调出本层

    • for+continue

      for循环调出本层,进入下次循环。

    • for循环的嵌套

      外层循环循环一次,内层循环循环所有的

      for i in range(3):
          print(f'-----:{i}')
          for j in range(2):
              print(f'*****:{j}')
      -----:0
      *****:0
      *****:1
      -----:1
      *****:0
      *****:1
      -----:2
      *****:0
      *****:1
      
    • for+else

      for循环没有break的时候触发else内部代码块

      name_list = ['nick', 'jason', 'tank', 'sean']
      for name in name_list:
          print(name)
      else:
          print('for循环没有被break中断掉')
      nick
      jason
      tank
      sean
      for循环没有break中断掉
      
    • for循环实现loading

      import time
      
      print('Loading', end='')
      for i in range(6):
          print(".", end='')
          time.sleep(0.2)
      Loading......
      

  • 相关阅读:
    已知二叉树的前序遍历、中序遍历,求后序遍历
    排序算法
    应用层
    运输层
    mysql分页查询
    mysql 性能分析及explain用法
    数据库SQL优化
    MATLAB GUI设计(1)
    路径选择
    Python itchat库(1)
  • 原文地址:https://www.cnblogs.com/1naonao/p/10907919.html
Copyright © 2011-2022 走看看