for循环
在Python中,for循环是一种迭代结构,用于遍历可迭代对象(如列表、元组、字符串、字典、集合等)中的元素。
for循环可以解决while循环的局限性
#while循环在遍历数据时的局限性
如下for循环可以实现
lst = [1,2,3,4,5] # ok i = 0 while i < len(lst): print(lst[i]) i+=1
用while循环无法实现:
setvar = {"a","b","c"} # not ok i = 0 while i < len(setvar): print(setvar[i]) i+=1
1.for循环的基本语法
Iterable 可迭代性数据:1.容器类型数据 2.range对象 3.迭代器
for 变量 in Iterable: code1
#字符串 container = "北京和深圳温差大概20多度" #列表 container = [1,2,3,4,4,5] #元组 container = ("孙开洗","孙健","孙悟空") #集合 container = {"陈璐","曹静怡","王志国","邓鹏","合力"} #字典 container = {"cl":"风流倜傥","cjy":"拳击选手","wzg":"寻花问柳","dp":"帅气,祖国的栋梁","hl":"你是个好人"}
#遍历数据
for i in container: print(i)
(1)遍历不等长多级容器
container = [1,2,3,4,("嗄","234",{"马春配","李虎凌","刘子涛"})] for i in container: # 判断当前元素是否是容器,如果是,进行二次遍历,如果不是,直接打印 if isinstance(i,tuple): # ("嗄","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)遍历等长的容器
container = [("马云","小马哥","马春配") , ["王健林","王思聪","王志国"],{"王宝强","马蓉","宋小宝"}] for a,b,c in container: print(a,b,c)
2.变量的解包
a,b,c = "poi" a,b = (1,2) a,b = 1,2 a,b,c = [10,11,12] a,b = {"林明辉","家率先"} a,b = {"lmh":"林明辉","jsx":"家率先"} a,b,c = ("马云","小马哥","马春配") print(a,b,c)
3.for…else【详细讲解】
for 临时变量 in 序列: 重复执行的代码 ...... else: 循环正常结束后要执行的代码
所谓else指的是循环正常结束后要执行的代码,即如果是break终止循环的情况。else下方缩进的代码将不执行。
4.range对象
range([开始值,] 结束值 [,步长])
取头舍尾,结束值本身获取不到,获取到它之前的那一个数据
#range(一个值) 默认从0开始 for i in range(5): # 0 ~ 4 print(i)
#range(二个值) for i in range(3,8): # 3 4 5 6 7 print(i) #range(三个值) 正向的从左到右 for i in range(1,11,3): # 1 4 7 10 print(i) #range(三个值) 逆向的从右到左 for i in range(10,0,-1): # 10 9 8 7 ... 1 print(i)
5.总结
while 一般用于处理复杂的逻辑关系
for 一般用于迭代数据
部分情况下两个循环可以互相转换;
i = 1 while i <= 9: j = 1 while j <= i: print("%d*%d=%2d " % (i,j,i*j) ,end="" ) j+=1 print() i +=1 for i in range(1,10): for j in range(1,i+1): print("%d*%d=%2d " % (i,j,i*j) ,end="" ) print()
for循环乘法口诀
方向二:
方向三:
方向四:
6.打印 1 ~ 10 跳过5
i = 1 while i <= 10: if i == 5: i += 1 continue print(i) i +=1 for i in range(1,11): if i == 5: continue print(i)
7.打印菱形小星星
* *** ***** ******* ********* *********** *********** ********* ******* ***** *** *
空格 + 星星 + 换行
总行数:
对于最大行任意个星星n ,总行数: n // 2 + 1
13 -> 7
11 -> 6
9 -> 5
7 -> 4
空格:
对于当前行i , 空格数量 = 总行数 - 当前行
1 => 5
2 => 4
3 => 3
4 => 2
5 => 1
6 => 0
星星:
对于当前行i , 星星数量 = 2 * 当前行 - 1
1 => 1
2 => 3
3 => 5
4 => 7
n = 13 hang = n // 2 + 1 i = 1 while i <= hang: # 打印空格 kongge = hang - i print(" " * kongge, end="") # 打印星星 xingxing = 2 * i - 1 print("*" * xingxing, end="") # 打印换行 print() i += 1 i = hang while i >= 1: # 打印空格 kongge = hang - i print(" " * kongge, end="") # 打印星星 xingxing = 2 * i - 1 print("*" * xingxing, end="") # 打印换行 print() i -= 1
方法二:
#打印菱形
* *** ***** ******* ********* *********** *********** ********* ******* ***** *** *
最大行星星数n
n = 2*hang - 1 kongge = hang - i xingxing = 2*i -1
n = 13 hang = int((n + 1) / 2) i = 1 while i <= hang: kongge = hang - i print(" " * kongge,end='') xingixng = 2*i - 1 print(f"*"* xingixng,end='') print() i += 1 i = hang while i >= 1: kongge = hang - i print(" " * kongge,end='') xingixng = 2*i - 1 print(f"*"* xingixng,end='') print() i -= 1
菱形二
* * * * * * * * * * * * * * * * * * * *
行数 n 星星 n
kongge = n - i
hang = 5 i = 1 while i <= hang: kongge = hang - i print(f" " * kongge,end='') xingxing = i if i == 1: print("*",end='') else: j = 1 while j <= 2*i -1: if j % 2 == 1: print("*",end='') else: print(" ",end='') j += 1 print() i += 1 i = hang while i >= 1: kongge = hang - i print(f" " * kongge,end='') xingxing = i if i == 1: print("*",end='') else: j = 1 while j <= 2*i -1: if j % 2 == 1: print("*",end='') else: print(" ",end='') j += 1 print() i -= 1