Tuple indexing retrieves an object by position with square brackets. Counting starts at index 0, so the first object is at position 0 and the second at position 1. section 10.2
Indexing through nested tuples
The expression (3, (3, ("5", 7), 9), "a")[1][1][1] evaluates to 7. First, [1] selects (3, ("5", 7), 9); the next [1] selects ("5", 7); the final [1] selects 7. section 10.2
Remember these related operations:
- Indexing can return a primitive object or another tuple. section 10.2
- Apply another index immediately when the selected object is itself a tuple. section 10.2
- Use slicing with the same rules as string slicing, while remembering that a position may contain another tuple. section 10.2
Figure 10.1. The structure of the tuple (3, (3, ("5", 7), 9), "a"). The dashed lines indicate separate objects in the tuple.
Visualize each nested tuple as a separate object before following the indexing operations. section 10.2
