while 一般用于处理复杂的逻辑关系
while的基本语法

  while condition:
    # 代码块
    # 当 condition 为 True 时,执行这里的代码

基本循环:

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

for 一般用于迭代数据
部分情况下两个循环可以互相转换;

for循环的用法:
tips: Iterable可迭代性数据:1.容器类型数据 2.range对象 3.迭代器
for 变量 in Iterable:
代码块

1.遍历容器

# 字符串
container = "北京和深圳温差大概20多度"
# 列表
container = [1,2,3,4,5]
# 元组
container = ("孙开洗","孙健","孙悟空")
# 集合 
container = {"陈璐","曹静怡","王志国","邓鹏","何莉"}
# 字典
container = {"google":"谷歌", "baidu":"百度", "tesla":"特斯拉", "pepsi":"百事", "redbull":"红牛"}

# 遍历数据
for i in container:
    print(i)

2.变量的解包

a,b,c = ("马云", "小马哥", "马冬梅")
print(a, b, c)
#结果是: 马云 小马哥 马冬梅

所以在遍历等长的容器时可以这样做:

container = [("马云", "小马哥", "马冬梅") , ["王健林", "王思聪", "王志国"],{"王宝强", "马蓉", "宋小宝"}]
for a, b, c in container:
    print(a, b, c)
#结果是: 
马云 小马哥 马冬梅
王健林 王思聪 王志国
马蓉 王宝强 宋小宝

#一般情况:
#1 遍历不等长多级容器时,我们需要先判断容器的类型
container = [1, 2, 3, 4, ("嗄", "234", {"马春配", "李虎凌", "刘子涛"})]
for i in container:
    # 判断当前元素是否是元组,如果是就进行二次遍历, 如果不是则直接打印
    if isinstance(i, tuple):
        # i = ("嗄","234",{"马春配","李虎凌","刘子涛"})
        for j in i:
            # 判断当前元素是否是集合,如果是,进行三次遍历,如果不是,直接打印
            if isinstance(j, set):
                # j = {"马春配","李虎凌","刘子涛"}
                for k in j:
                    print(k)
            else:
                print(j)
                
    # 打印数据
    else:
        print(i)



# 2.遍历不等长多级容器
container = [("刘玉波", "历史源", "张光旭"), ("上朝气", "于朝志"), ("韩瑞晓",)]
for i in container:
    for j in i:
        print(j)

3.range对象

range([开始值, 结束值 ,步长])
#和字符串的切片一样"取头舍尾"

# range(一个值)
for i in range(5): # 0 ~ 4
    print(i)

# range(二个值)
for i in range(3,8): # 3~7: 3 4 5 6 7 
    print(i)

# range(三个值) 正向的从左到右
for i in range(1,11,3): #1~10, 中间步长是3 1 4 7 10 
    print(i)

# range(三个值) 逆向的从右到左
for i in range(10,0,-1): # 10 9 8 7 ... 1 
    print(i)

3.迭代器

待学习....