Exercise 34

Given the list of student scores: scores = [12, 4, 14, 11, 18, 13, 7, 10, 5, 9, 15, 8, 14, 16]. Write a Python program that allows you to extract from this list another list that contains only the scores above 10 ( scores >= 10 ).

Solution

scores = [12, 4, 14, 11, 18, 13, 7, 10, 5, 9, 15, 8, 14, 16]

# create a new list containing only the scores above 10
above_10_scores = [score for score in scores if score >= 10]

# print the above-10 scores
print(above_10_scores)
"""
Here's the output of running this program:

[12, 14, 11, 18, 13, 10, 15, 14, 16]
"""





Explanation:

  1. First, we define the list scores: that contain the student scores.
  2. We create a new list above_10_scores: using a list comprehension. The list comprehension iterates over all scores in the scores list, and includes only those scores that are greater than or equal to 10.
  3. Finally, we print the above_10_scores: list using the built-in print function.

As you can see, the program correctly extracts the scores above 10 from the given list of student scores.

 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 34: extract from list scores, only the scores above the average with Python”

Leave a Reply