首页 > 编程语言 >python的for循环

python的for循环

时间:2022-11-13 12:58:06浏览次数:38  
标签:遍历 python price dic 33 循环 print else

语法

for i in xxx
    # 循环体
else:
    # 循环正常结束执行执行else中的代码
    # 循环通过break强制结束则不会执行else    

用法

1、遍历列表

li = [34, 66, 33, 78, 66, 90, 78, 87]
for price in li:
    if price > 60:
        print("您的成绩为{},及格".format(price))
    else:
        print("您的成绩为{},不及格".format(price))

2、遍历字典

1.遍历字典的键

dic = {"aa": 11, "bb": 22, "cc": 33, "dd": 44}
for i in dic:
    print(i)

2.遍历字典的值

dic = {"aa": 11, "bb": 22, "cc": 33, "dd": 44}
for i in dic.values():
    print(i)

3.遍历字典的键和值

dic = {"aa": 11, "bb": 22, "cc": 33, "dd": 44}
for k, v in dic.items():
    print("key:", k, "value:", v)

3、与range()搭配遍历

for i in range(10):
    if i % 2 == 0:
        print(i)

标签:遍历,python,price,dic,33,循环,print,else
From: https://www.cnblogs.com/nikeairball/p/16885783.html

相关文章