python列表中if语句的用途

809次阅读
没有评论

python列表中if语句的用途

1、在遍历的时候对特殊元素进行筛查,即使用for循环可以对列表中的元素进行遍历

scores = [100, 98, 59, 60, 78, 99, 57, 63]
flag = 0
for score in scores:
    if score <= 60:
        flag += 1
 
print('共有 ' + str(flag) + ' 门课程不及格。')

2、用于对列表是否为空进行判断。当列表初始值为空,这时再执行循环没有意义。

scores = []
flag = 0
if scores:
    for score in scores:
        if score <= 60:
            flag += 1
    print('共有 ' + str(flag) + ' 门课程不及格。')
else:
    print('暂时没有成绩公布')
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-07-19发表,共计384字。
新手QQ群:570568346,欢迎进群讨论 Python51学习