首页 > 文章列表 > python列表中if语句的用途

python列表中if语句的用途

Python if语句 列表
126 2022-08-07

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('暂时没有成绩公布')

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