Tuples give us various ways to access their elements. These are as follows:
Indexing
Slicing
Similar to lists, we can use the index operator [] to access an element in a tuple by using its index. Tuple indices start at zero, just like those of lists.
In this exercise, you will see how to use indexing to access tuples:
Create a new pets tuple with the elements dog, cat, and parrot:
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux pets = 'dog', 'cat', 'parrot'
Run pets[1] to access the second index:
pets[1] => 'cat'
Try to access an index that is not in the tuple. Python will raise an IndexError, as shown here:
pets[3] Traceback (most recent call last): File "python", line 1, in <module> IndexError: tuple index out of range
Indices can also be negative. If you use a negative index, -1 will reference the last element in the tuple, -2 will refer to the second from last element in the...