Python知识点2:循环语句(while、for、break、continue)

tech2022-08-16  132

Python知识点2:循环语句(while、for、break、continue) b站学习:https://www.bilibili.com/video/BV12E411A7ZQ 注意:三引号只是把代码注释掉了

一、循环语句(while、for、break、continue)

#for循环 ''' #数的遍历 for i in range(5): print(i) for i in range(0,10,3): print(i) for i in range(-10,-100,-30): #反向也可以 print(i) ''' ''' #字符串的遍历 name="vivian" for x in name: print(x,end="\t") ''' ''' #列表的遍历 a=["aa","bb","cc","dd"] for i in range(len(a)): print(i,a[i]) ''' #while循环 ''' i=0 while i<5: print("当前是第%d次循环"%(i+1)) print("i=%d"%i) i+=1 ''' ''' #1-100求和 sum=0 i=1 while i<=100: sum+=i i+=1 print("1到100的和为:%d"%sum) ''' ''' #while可以与else搭配 count=0 while count<5: print(count,"小于5") count+=1 else: print(count,"大于或等于5") ''' ''' #break&continue i=0 while i<10: i=i+1 print("-"*30) if i==5: break #continue print(i) '''

二、课后练习(打印九九乘法表)

#课后练习:打印九九乘法表 for a in range(1,10): for b in range(1,10): print(a,"*",b,"=",a*b,end="\t") if a==b: print("\n") break
最新回复(0)