10.4. 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.
Check your understanding
- 5.2
- 5.2 is at index 4.
- 3.14
- Yes, 3.14 is at index 5 since we start counting at 0 and sublists count as one item.
- 8.8
- 8.8 is at index 6.
list-4-1: What is printed by the following statements?
a_list: [float]
a_list = [3.2, 67.1, 9.8, 2.0, 5.2, 3.14, 8.8]
print(a_list[5])
You have attempted of activities on this page