python中循环与使用场景

1.while 循环

counter =1
while counter <= 10:
    counter += 1
    print(counter)
else:
    print("EOF")

2.for 循环

a=[['apple','orange','banana','grape'],(1,2,3)]
for x in a:
    for y in x:
        print(y,end=',')
else:
    print("fruit is gone",end='\n')

3.break 和 continue

a = [1,2,3,4,5]
for x in a:
    if x ==2:
        continue
    print(x)   # 1,3,4,5
else:
    print("EOF")

print("===============")


a = [1,2,3,4,5]
for x in a:
    if x ==2:
        break
    print(x)   # 1
else:
    print("EOF")

print("===============")

for x in range(10,0,-1):
    print(x)

posted @ 2022-05-29 18:06  repinkply  阅读(40)  评论(0)    收藏  举报