Index

Accessing Elements in a List:

You reference a list element by writing the list’s name followed by the element’s index (that is, its position number) enclosed in square brackets ([]). The following diagram shows the list c labeled with its element names:

The first element in a list has the index 0. So, in the five-element list c, the first element is c[0] and the last is c[4]:

print(c[0])
print(c[4])

Indices must be integer values or expressions that when evaluated, result in an integer value.

a = 0
b = 1
listyPoo = [-5.3, -6.7, -1.2]
print(listyPoo[a + b])

Using a non-integer value will result in a TypeError.

Accessing Values Backwards:

Lists also can be accessed from the end by using negative indices: ```python seq = [4.7, 6.8] print(seq[-1]) # Displays 6.8 ```

Length of Lists

To get a list’s length, use the built-in `len` function: ```python example = [1, 4, 2, 0] print(len(example)) ```

You can utilize this function when you are trying to access elements at the end of a list.

Accessing Nonexistent Elements:

Using an out-of-range value for your index value will result in an IndexError: