>_Code Club
← All exercises

python

Lists

A list holds several values, in order, between square brackets:

scores = [12, 7, 30, 18]

print(scores[0])     # the first item: 12 (counting starts at 0!)
print(len(scores))   # how many items: 4

scores.append(25)    # add an item at the end

for score in scores:
    print("Score:", score)

Your mission

Python already has sum() and max(), but the goal is to understand how they work. Write them yourself using a loop:

  1. total(numbers) returns the sum of all the numbers. An empty list gives 0.
  2. biggest(numbers) returns the largest number of a (non-empty) list.

Hint for biggest: start with the first item as "the biggest so far", then compare it with every other item.

Next exercisePlaying with text →
Loading the editor…