学生成绩统计代码Python
时间: 2024-11-23 15:25:01 浏览: 71
在Python中,我们可以编写一段简单的代码来统计学生成绩。这里假设我们有一个包含学生ID和成绩的数据结构,比如列表或字典。以下是一个基础示例:
```python
# 定义一个学生成绩数据结构,例如字典
students = [
{"id": 1, "score": 90},
{"id": 2, "score": 85},
{"id": 3, "score": 95},
{"id": 4, "score": 78},
# 更多学生...
]
# 统计总分、平均分以及最高分和最低分
total_score = sum(student["score"] for student in students)
average_score = total_score / len(students)
def find_max_min(scores):
max_score = max(scores, key=scores.get)
min_score = min(scores, key=scores.get)
return max_score, min_score
max_score, min_score = find_max_min({student["id"]: student["score"] for student in students})
print("总分:", total_score)
print("平均分:", average_score)
print("最高分:", max_score, "学生ID:", [id for id, score in students if score == max_score][0])
print("最低分:", min_score, "学生ID:", [id for id, score in students if score == min_score][0])
阅读全文