# 计算机二级Python考点解析8

1. for循环

（1）列表遍历循环

names = ['Michael', 'Bob', 'Tracy']

for name in names:

print(name)

Michael

Bob

Tracy

sum = 0

for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:

sum = sum + x

print(sum)

55

（2）字典遍历循环

l = {'name1' : 'zhangsan', 'name2' : 'lisi', 'name3' : 'wangwu'}

for key in l:

print (key, ' value : ',l[key])

name1 value :zhangsan

name2 value : lisi

name3 value : wangwu

for key, value in l.items():

print (key, ' value : ', value)

name1 value :zhangsan

name2 value : lisi

name3 value : wangwu

2. while循环

sum = 0

n = 10

while n > 0:

sum = sum + n

n = n - 1

print(sum)

55

break

n = 1

while n

print(n)

n = n + 1

print('END')

1

2

...

9

10

END

n = 1

while n

if n > 5: # 当n = 11时，条件满足，执行break语句

break # break语句会结束当前循环

print(n)

n = n + 1

print('END')

END

continue

n = 0

while n

n = n + 1

print(n)

n = 0

while n

n = n + 1

if n % 2 == 0: # 如果n是偶数，执行continue语句

continue # continue语句会直接继续下一轮循环，后续的print()语句不会执行

print(n)

E

N

D

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180825G1LLE500?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2021-12-02

2021-12-02

2021-12-02

2021-12-02

2021-12-02

2021-12-02

2018-03-16

2021-12-02

2021-12-02

2021-12-02

2021-12-02

2021-12-02