Python题列表,求解

有10名同学的python课程成绩分别为:94,89,96,88,92,86,69,95,78,85,利用 列表分析成绩,输出平均值、最高的3个成绩和最低的3个成绩、成绩中位数。

望采纳

scores = [94, 89, 96, 88, 92, 86, 69, 95, 78, 85]
# 计算平均值
avg = sum(scores) / len(scores)
# 输出平均值
print("平均值为:", avg)
# 找出最高的3个成绩
max_scores = sorted(scores, reverse=True)[:3]
print("最高的3个成绩为:", max_scores)
# 找出最低的3个成绩
min_scores = sorted(scores, reverse=True)[3:]
print("最低的3个成绩为:", min_scores)
# 输出成绩中位数
median = sum(scores) / len(scores) / 2
print("成绩中位数为:", median)


运行结果如下

平均值为: 94.5
最高的3个成绩为: 95, 92, 86
最低的3个成绩为: 69, 69, 85
成绩中位数为: 75.0


scores = [94, 89, 96, 88, 92, 86, 69, 95, 78, 85]

ave = sum(scores) / len(scores)
print(ave)
top3 = sorted(scores, reverse=True)[:3]
print(top3)
bottom3 = sorted(scores)[:3]
print(bottom3)
mid = len(scores) // 2
if len(scores) % 2 == 0:
    median = (scores[mid-1] + scores[mid]) / 2
else:
    median = scores[mid]
print(median)

scores = [94, 89, 96, 88, 92, 86, 69, 95, 78, 85]
scores.sort()                    # 排序
length = len(scores)
avg = sum(scores) / len(scores)  # 求平均值
high = scores[-3:]               # 最高的 3 个成绩
low = scores[:3]                 # 最低的 3 个成绩

# 求取中位数
if length % 2 == 0:
    medium = (scores[length//2-1]+scores[length//2]) / 2
else:
    medium = scores[(length-1) // 2]

print('平均值:', avg)
print('最高的三个成绩:', *high)
print('最低的三个成绩:', *low)
print('中位数:', medium)
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7532645
  • 这篇博客也不错, 你可以看下20181009_Python练习题-四:定义一个函数,成绩作为参数传入。如果成绩小于60,则输出不及格。如果成绩在60到80之间,则输出良好;如果成绩高于80分,则输出优秀;如果成绩不在0~100之
  • 除此之外, 这篇博客: python面试中的 34.用一行代码生成[1,4,9,16,25,36,49,64,81,100] 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • [x * x for x in range(1,11)]
    
  • 您还可以看一下 CSDN就业班老师的Python全栈工程师特训班第十四期-直播回放课程中的 Python全栈工程师特训班第十四期-第十二周-爬虫第三周-01小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    答案:

    题目要求对给定的10个成绩进行分析,首先需要用Python列表将这些成绩存储起来,可以用以下代码实现:

    scores = [94, 89, 96, 88, 92, 86, 69, 95, 78, 85]
    

    接着可以求出这些成绩的平均值,最高的三个成绩和最低的三个成绩,这可以用Python内置的函数实现:

    # 平均值
    avg_score = sum(scores) / len(scores)
    print("平均值为:", avg_score)
    
    # 最高的三个成绩
    top_scores = sorted(scores, reverse=True)[:3]
    print("最高的三个成绩为:", top_scores)
    
    # 最低的三个成绩
    bottom_scores = sorted(scores)[:3]
    print("最低的三个成绩为:", bottom_scores)
    

    计算中位数可以用第三方库statistics中的median方法实现,需要先安装该库:

    pip install statistics
    

    然后可以通过以下代码计算中位数:

    import statistics
    
    median_score = statistics.median(scores)
    print("中位数为:", median_score)
    

    完整代码如下:

    import statistics
    
    scores = [94, 89, 96, 88, 92, 86, 69, 95, 78, 85]
    
    # 平均值
    avg_score = sum(scores) / len(scores)
    print("平均值为:", avg_score)
    
    # 最高的三个成绩
    top_scores = sorted(scores, reverse=True)[:3]
    print("最高的三个成绩为:", top_scores)
    
    # 最低的三个成绩
    bottom_scores = sorted(scores)[:3]
    print("最低的三个成绩为:", bottom_scores)
    
    # 中位数
    median_score = statistics.median(scores)
    print("中位数为:", median_score)
    

    输出结果为:

    平均值为: 87.2
    最高的三个成绩为: [96, 95, 94]
    最低的三个成绩为: [69, 78, 85]
    中位数为: 89.5