• Since list is an ordered set, we can use a list to represent three students in the class in descending order of scores:

    >>> L = ['Adam', 'Lisa', 'Bart']
  • So how do we get the NTH student from the list? The method is to get the specified element in a list by index. It is important to note that the index starts at 0, that is, the first element has an index of 0, the second element has an index of 1, and so on.
  • So, to print the name of the first student, use L[0]:
>>> print L[0]
Adam
Copy the code
  • To print the name of the second student, use L[1]:
>>> print L[1]
Lisa
Copy the code
  • To print the name of the third student, use L[2]:
>>> print L[2]
Bart
Copy the code
  • To print the name of the fourth student, use L[3]:
>>> print L[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
Copy the code

An error! IndexError means that the index is out of range, because the list above has only three elements. The valid indexes are 0, 1, and 2. So, when using indexes, be careful not to overstep the bounds.