发布时间:2023-01-29 09:36:26 人气:60 作者:多测师
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('暂时没有成绩公布')
以上就是python列表中if语句的用途,希望对大家有所帮助。更多Python学习指路:请关注多测师。https://www.e70w.com/xwzx/