while循环事例
下面这段为while死循环'''
CONDITION = True
while CONDITION:
print('i am while')
'''
counter = 1
while counter <=10:
counter += 1
print(counter)
else:
print('EOF')
for循环事例
主要是用来遍历、循环序列或者集合、字典'''
a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
for y in x:
print(y)
else:
print('fruet is gone')
'''
只打印a中的1,2
'''
a = [1,2,3]
if x == 3:
break
print(x)
'''
只跳过a中的2
a = [1,2,3]
for x in a:
if x == 2:
continue
print(x)
range循环事例
'''
for x in range(0,10):
print(x)
'''
'''
for x in range(0,10,2):
print(x)
'''
一般默认是竖着打印出来,如果想横着打印用end='' 如果想横着打印,且中间用|隔开,则用end='|' 想生成递减等差数列
for x in range(10,0,-2):
print(x,end='|')
循环部分思考题
要求打印a中的1,3,5,7 要使用三种方法a = [1,2,3,4,5,6,7,8]
for x in a[0:8:2]:
print(x)
for i in range(0,len(a),2):
print(a[i])
b = a[0:len(a):2]
print(b)