21-while-break
break是结束循环,break之后、循环体内代码不再执行。
while True:
yn = input('Continue(y/n): ')
if yn in ['n', 'N']:
break
print('running...')
22-while-continue
计算100以内偶数之和。
continue是跳过本次循环剩余部分,回到循环条件处。
sum100 = 0
counter = 0
while counter < 100:
counter += 1
# if counter % 2:
if counter % 2 == 1:
continue
sum100 += counter
print(sum100)
23-for循环遍历数据对象
astr = 'hello'
alist = [10, 20, 30]
atuple = ('bob', 'tom', 'alice')
adict = {'name': 'john', 'age': 23}
for ch in astr:
print(ch)
for i in alist:
print(i)
for name in atupl