This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 2: Lists

Accessing elements in a list

face Josiah Wang

Each item in a list is called an element.

You can access each element by referring to its index.

The index starts from 0 (i.e. the first item’s index is 0).

Lists, elements and indices

>>> students = ["Abraham", "Bella", "Connor", "Da-ming", "Enya"]
>>> print(students[0])       # First element: "Abraham"
Abraham
>>> print(students[1])       # Second element: "Bella"
Bella

Python also allows you to access elements in a list in a reverse order, with negative indices.

Negative indices

>>> print(students[-1])      # First element from the end
Enya
>>> print(students[-2])      # Second element from the end
Da-ming