Lesson 7 of 14 · 9 min

Lists

A list holds values in order, inside square brackets:

fruits = ["mango", "banana", "guava"]
print(fruits[0])        # mango   (counting starts at 0)
print(fruits[-1])       # guava   (negative counts from the end)
print(len(fruits))      # 3
fruits.append("orange") # add to the end
fruits[1] = "papaya"    # replace an item

Looping over a list

for fruit in fruits:
    print(fruit)

Slices

A slice takes part of a list: start:stop, where stop is not included.

numbers = [10, 20, 30, 40, 50]
numbers[1:3]   # [20, 30]
numbers[:2]    # [10, 20]   from the start
numbers[3:]    # [40, 50]   to the end

Useful built-ins

sum(numbers), min(numbers), max(numbers), len(numbers), and sorted(numbers), which returns a new sorted list (sorted(numbers, reverse=True) for biggest first). numbers.sort() sorts the list itself instead.

Your task

Write three functions that take a list of exam scores:

  1. average(scores): the mean score.
  2. top_three(scores): the three highest scores, highest first, without changing the original list.
  3. passed(scores): a new list of the scores that are 50 or more, in their original order. Build it with a loop and append.
main.py
def average(scores):
    pass


def top_three(scores):
    pass


def passed(scores):
    pass


scores = [55, 92, 38, 74, 81, 49]
print(average(scores))
print(top_three(scores))
print(passed(scores))

Python 3.14 runs right here in your browser. The first run downloads about 10 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…