Python:Accessing an Array

From Progzoo
Jump to: navigation, search

You access a list using the square bracket notation.

The first element of list x is x[0]


[Font] [Default] [Show] [Resize] [History] [Profile]

The last element of list x is x[-1]

Negative numbers indicate index from the end of the list:


[Font] [Default] [Show] [Resize] [History] [Profile]

Out of Bounds

If you try to read past the end of a list an exception IndexError is raised.


[Font] [Default] [Show] [Resize] [History] [Profile]