3.1 Lists and arrays

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

3.1 Lists and arrays

A list holds multiple values in order. That’s useful for high scores, items, or anything you need to keep in a sequence.

Creating a list

Use square brackets and commas:

scores = [100, 85, 92, 78]
names = ["Alex", "Sam", "Jordan"]
mixed = [10, "hello", True]

Indexing (position)

Positions start at 0, not 1:

You can also count from the end: scores[-1] is the last item, scores[-2] the second-to-last.

Changing and adding

Looping over a list

for score in scores:
    print(score)

Or with index:

for i in range(len(scores)):
    print("Position", i, ":", scores[i])

Slicing

Get a range of items: scores[1:3] gives the items at index 1 and 2 (not 3). So [85, 92].

Mini project: High scores

  1. Start with a list of 5 scores.
  2. Print each score and its position (1st, 2nd, …).
  3. Add a new score with append.
  4. Print the highest score (you can use max(scores) or a loop).

Next: 3.2 Dictionaries and objects — key–value pairs for lookups.