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:
average(scores): the mean score.top_three(scores): the three highest scores, highest first, without changing the original list.passed(scores): a new list of the scores that are 50 or more, in their original order. Build it with a loop andappend.