Tag: Ack Index des access specifies Lin pre first so
Python accesses list by index
Since list is an ordered set, we can use a list to indicate from high to low the 3 students in the class:
>>> L = [' Adam ', ' Lisa ', ' Bart ']
So how do we get the names of the nth-named students from the list? The method is to get the specified element in the list by index.
It is important to note that the index starts at 0 , that is, the index of the first element is 0, the second element is indexed by 1, and so on.
Therefore, to print the first name of a classmate, use L[0]:
>>> Print L[0]adam
To print the name of the first two students, use L[1]:
>>> Print L[1]lisa
To print the name of the first three students, use L[2]:
>>> Print L[2]bart
To print the name of the first four students, use L[3]:
>>> Print L[3]traceback (most recent call last): File "<stdin>", line 1, in <module>indexerror:l Ist index out of range
The error! Indexerror means that the index is out of range because the list above has only 3 elements, and the valid index is 0,1,2.
Therefore, when using an index, be careful not to cross the border.
Python accesses list by index