循环:
while
for
什么是循环:周而复始
代码尽量不要重复----》
while
书写格式
while 表达式:
循环体
通过条件不断变化,从而确定你的循环体是否继续执行
while Ture:
循环体
index = 1
while index <= 100:
if index == 50:
break # 立即结束循环
print("hello!!!")
index += 1
index = 1
while index <= 100:
index += 1
if index == 50:
continue # 终止本次循环提前开始下一次循环
print(index)
python中缩进控制语句的范围
while----else
循环正常结束会执行else中的代码,针对break关键字的
练习求1-100的和
a = 1
b = 0
while a <= 100:
b += a
a += 1
print(b)
循环嵌套
for迭代容器中的每一个元素
for临时变量 in 数据容器:
代码块
for循环不仅可以迭代无序的容器,也能迭代有序的容器
range(start,end,step):生成一系列连续的整数
range(num):[o-num]
range(start,end)
b=1
while b<=a:
c=1
while c<=b:
print("*",end="")
c+=1
print()
b+=1


本文详细介绍了Python中的while和for循环结构,包括循环的基本概念、书写格式、条件控制(break和continue),以及循环嵌套和range函数的用法。通过实例演示了如何避免代码重复并理解循环的逻辑。
1658

被折叠的 条评论
为什么被折叠?



