python基础语法 004-3流程控制- while

1 while 

while 
主要用的场景没有 for 循环多。
while循环:主要运行场景
我不知道什么时候结束。。。不知道运行多少次

1.1 基本用法

# while 4 > 3: #一直执行
#     print("hell0")
    
while 4 < 3: #不会打印,什么都没有
    print("hell0")

while 循环的执行流程

#while 循环的执行流程
#当把while 循环下面的子分支执行完毕之后,
#程序会返回while 条件判断语句。直至条件不满足
#while趋势时一个加强版的if, if判断一次,while会回头再判断条件是否满足
#可以用debug执行 
while 4 > 3:
    print("hell0")
    print("bye")
    print("no")

条件赋值 

#条件赋值,满足什么条件后跳出循环
#while循环把for循环的机制手动化
cases = [
    {"url" : "http://", "method": "get"},
    {"url" : "http://examle", "method": "post"}
]

index = 0
while index < len(cases):
    print(cases[index])
    #自动索引 + 1
    #需要缩进,不然不在循环中,会导致死循环
    index += 1

--------结果---------
{'url': 'http://', 'method': 'get'}
{'url': 'http://examle', 'method': 'post'}

 1.2 break 

一般条件控制好,是不需要使用break的,但存在不好控制的条件。

cases = [
    {"url" : "http://", "method": "get"},
    {"url" : "http://examle", "method": "post"}
]

index = 0
while True:
    print(cases[index])
    if index == 1:
        #手工终止,强制终止while循环或者for
        print("索引为{},终止while循环".format(index))
        break
    index += 1

-------------结果-------
{'url': 'http://', 'method': 'get'}
{'url': 'http://examle', 'method': 'post'}
索引为1,终止while循环

 1.3 pass

通常用在,子语句不做任何操作,用pass放在这作为一个占位符 ,表示什么都不干


pass语法
while True:
    #无限跑abc
    pass
    print("abc")




if 1:
    #当有冒号有子语句的时候,目前还不知道这个语句怎么写,先用pass占一个位置
    pass
    print("hell0")
elif 2:
    print("bey")

1.4  continue

 终止本次子语句

#continue 是表示跳过此次子与,进入下一个循环判断
while True:
    continue
    print("abc")

1.5 while循环的嵌套

while True:
    print("第一层")
    while True:
        print("the 第二层")

while 小结 

while和for区别

相关推荐

  1. Python基础流程控制语句

    2024-07-10 19:22:05       29 阅读
  2. Python流程控制语句

    2024-07-10 19:22:05       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 19:22:05       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 19:22:05       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 19:22:05       45 阅读
  4. Python语言-面向对象

    2024-07-10 19:22:05       55 阅读

热门阅读

  1. ES6 之 Promise 构造函数知识点总结 (四)

    2024-07-10 19:22:05       24 阅读
  2. 软件工程需求之:业务需求与用户需求

    2024-07-10 19:22:05       16 阅读
  3. 学习数据库的增删改查

    2024-07-10 19:22:05       17 阅读
  4. oracle 数据更新procedure 模板

    2024-07-10 19:22:05       20 阅读
  5. 【LeetCode 0050】【分治/递归】求x的n次方

    2024-07-10 19:22:05       20 阅读