您现在的位置是:课程教程文章

python for语句的应用场景

2023-12-13 23:33课程教程文章 人已围观

应用场景

1、当迭代遍历嵌套的数据类型时,例如,一个列表包含多个字典。

2、需求判断字典中是否有指定值。

3、如有,提示并退出循环。

4、如果不存在,希望在整个循环结束后得到统一的提示。

实例

students=[
{"name":"阿土",
"age":20,
"gender":True,
"height":1.7,
"weight":75.0},
{"name":"小美",
"age":19,
"gender":False,
"height":1.6,
"weight":45.0},
]

find_name="阿土"

forstu_dictinstudents:

print(stu_dict)

#判断当前遍历的字典中姓名是否为find_name
ifstu_dict["name"]==find_name:
print("找到了")

#如果已经找到,直接退出循环,就不需要再对后续的数据进行比较
break

else:
print("没有找到")

print("循环结束")

以上就是python for语句的应用场景,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

课程教程:python for语句的应用场景

上一篇:python如何模拟用户自动打卡

下一篇:没有了

站点信息

  • 文章统计篇文章