流程控制之while和for循环

流程控制之 while循环

流程控制之while循环基本概念

1、什么是循环

循环就是重复做某件事

2、为何要用循环

为了控制计算机能够像人一样重复做某件事

3、如何用循环

while 条件:
	代码1
    代码2
    代码3

基本语法:条件循环什么时候变成假就停止

流程控制之while循环的使用

  • 1、基本用法

    #打印0-5应该怎么做
    #基本语法
    # count=0
    # while True:
    #     print(count)
    #     count+=1
    
    #👇👇👇👇👇👇👇👇👇👇👇👇  注意while循环最主要的就是 条件的判断,所以条件如果是常量Ture,那就会一直循环。
    #所以如果把条件改成变量  当条件判定到为false时候就  这个时候while循环就终止
    # count=0
    # while count<6:
    #     print(count)
    #     count+=1
    
    #👇👇👇👇👇👇👇👇👇👇👇👇👇
    
    # count=0
    # tag=True
    # while tag :      # 当count=6时候  tag=False,程序还继续往下走,所以6就被打印出来了,再下一次while循环判断是False才终止
    #     if count==6:
    #         tag=False
    #     print(count)
    #
    #     count+=1
    #👇👇👇👇👇👇  #所以可以那么做  把条件为假程序运行的放在最后一步,那么就不会打印6。
    # count=0
    # tag=True
    # while tag :
    #     print(count)
    #     count+=1
    #     if count==6:
    #         tag=False
    
    
  • 2、死循环:条件永远为True

    #这像刚刚第一个代码展示的结果,条件为常量Ture,所以就一直打印了下去。
    这里面分为计算密集,和io密集,计算密集会很占用cpu。
    # while True:
    #     # x = input(">>> ")
    #     # y = input(">>> ")
    #     # x + y   #如果只运算用户输入的,是需要等待用户输入再去运算,大部分时间是等待接收状态。而不是系统一直往下运算这属于io密集
    
    #这种类型就是系统一直再重复运算+1结果再加,很占用cpu!,了解即可,在实际应用中我们大部分用到的都是io密集,是为了编写服务人类的程序
    count=0
    while True:
        print(count+1)
        count+=1
    
  • 3、结束while循环的两种方式

    方式一:把条件改为False

    特点:等到本次循环体代码运行完毕后,下一次循环判断条件时才会生效

    :等到本次循环体代码运行完毕后,下一次循环判断条件时才会生效

    #打印数字0-5
    # count=0
    # tag=True
    # while tag :      #,结果打印出来是0-6, 当count=6时候  tag=False程序还继续往下走,所以6就被打印出来了,再下一次while循环判断是False才终止
    #     if count==6:
    #         tag=False
    #     print(count)
    #
    #     count+=1
    

    方式二:break 代表结束本层循环

    特点:立即干掉while循环

    #打印数字0-5
    # count=0
    # tag=True
    # while tag :      
    #     if count==6:
    #         break	#同以一种方式,打印0-5  这次打印出来的结果就是0-5
    #     print(count)
    #
    #     count+=1
    

    两种方式的区别:

    把条件改为False 的话本次循环代码运行结束后,下一次循环判断条件时才会生效。

    而break是立即干掉while循环,后面的代码不会运行。

    两种代码在不同场景各有用处,分场景应用~

  • 3、嵌套多层的while循环

    先根据第二个知识点,说一下多层while循环应该怎么终止掉。

    *各有用处

    方式一:

    # tag = True
    # while tag:
    #     while tag:
    #         while tag:
    #             tag = False
    

    方式二:

    # while True:
    #     while True:
    #         while True:
    #             break
    #         break
    #     break
    

    就像if判断一样多层嵌套的方式 格式如上。

  • 4、练习题:模拟银行自动存取款机 ,登录之后,完成具体服务。

    #
    # while True:
    #     inp_user = input("username>>>: ")
    #     inp_pwd = input("password>>>: ")
    #
    #     if inp_user == "egon" and inp_pwd == "123":
    #         print('login successful')
    #         while True:
    #             print("""
    #             0 退出
    #             1 取款
    #             2 存款
    #             3 转账
    #             """)
    #             choice = input("请输入您的命令编号:")
    #             if choice == "0":
    #                 break
    #             elif choice == "1":
    #                 print("正在取款。。。")
    #             elif choice == "2":
    #                 print("正在存款。。。")
    #             elif choice == "3":
    #                 print("正在转账。。。")
    #             else:
    #                 print("输入的指令不存在")
    #         break
    #     else:
    #         print('username or password error')
    
  • 5、while+continue:结束本次循环,直接进入下一次循环

    #0-6打印不打印2和4
    #----------------------👇👇👇👇👇👇👇👇👇👇👇👇
    # count = 0
    # while count <6:
    #
    #     if count ==2 or count==4:
    #         count+=1
    #         continue  # continue同级别之后千万别写代码,写了也不会运行
    #     print(count)
    #     count+=1
    
    
    continue  #continue一定不要加在最后一步   (最后一行,和最后一步的概念)
    
  • 6、while +else的子代码块会在while循环正常死亡时运行,没有被break干掉就叫正常死亡

    count = 0
    while count < 5:
        print(count)
        count+=1
    else:
        print("====>")
    

    重点知识点总结:

    #终止while循环的两种方式:
    方式一:条件为假,把条件改为Fales
    特点:等到本次循环体代码运行完毕后,下一次循环判断条件时才会生效
    方式二:break
    特点:立即干掉while循环
    #嵌套多层的while循环如何终止:
    tag=False:要求是代码体同时都一致用的while tag,优点是可以同时终止掉所有的循环体
    单层break:需要每一层while循环都要break一次,但是每层while条件可以为多种多样
    #while+continue 
    结束本次循环,直接进入下一次循环
    注意:continue不要放在程序运行的最后一步,多余
    #while+else 当循环体自然死亡时候会运行else,没有被break就叫自然死亡
    

流程控制之for循环

流程控制之for循环基本概念

别名“取值循环”顾名思义

应用场景:

​ 在列表,如果元素过多,用索引查找不方便。

​ 字典中,是用kay来查找value,想要查找kay

流程控制之for循环的使用

  • 1、基本用法
# names=["william","egon","lili","jiji","tom","shimisi","xiaoming","xiaogang","xiaoguang","xiaofeng"]
for x in names:
    print(x)
    
# dic={"k1":111,"k2":222,"k3":333}
for x in dic
	print(x,dic[x])
    
for x,y in[["name","william"],["age","18"]]:
    print(x,y)
    
# for x in "hello":
#     print(x)
    
 
  • 2、for+break

    # for i in [11,22,33,44,55]:
    #     if i == 33:
    #         break
    #     print(i)
    
  • 3、for+continue

    # for i in [11,22,33,44,55]:
    #     if i == 33:
    #         continue
    #     print(i)
    
  • 4、for+else

    # for i in [11,22,33,44,55]:
    #     if i == 33:
    #         break
    #     print(i)
    # else:
    #     print('++++++++>')
    
  • 5、range()

    for x in range(3):
        print(1)
        print(2)
        #循环打印三次
    

重点知识点总结:

for+break
#终止循环查找
for+continue
#终止本次循环查找,开始新的循环
for+else
#for循环自然死亡的情况下正常运行,没有被break干掉就叫自然死亡
range()
#for循环基本原理是根据字典或者列表内元素次数多少重复运行多少次range相当于指定一个有多少元素的
C:\Users\William>python27
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range(5)
[0, 1, 2, 3, 4]
在python3中形式是,为了少占用内存空间的一种优化
可以称之为"老母鸡优化机制!"
print(range(5))
range(0, 5)
posted @ 2020-12-21 17:17  williamgess  阅读(145)  评论(0编辑  收藏  举报