Accessing ElementsΒΆ

The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string. We use the index operator ( [] – not to be confused with an empty list). The expression inside the brackets specifies the index. Remember that the indices start at 0. Any integer expression can be used as an index and as with strings, negative index values will locate items from the right instead of from the left.

Try to predict what will be printed out by the following code, and then run it to check your prediction. (Actually, it’s a good idea to always do that with the code examples. You will learn much more if you force yourself to make a prediction before you see the output.)

Next Section - List Membership