liveBook · Manning

Indexable type

As explained in Python Workout, Second Edition

An indexable type is a type whose values support access by an integer index or a slice.

An indexable type accepts either a single integer index or a slice. In Python, one function can therefore operate across strings, lists, and tuples instead of duplicating logic for each type. section 4.5

Process the final elements

Use a slice to select the last n values, then iterate over the result: mylist[-n:]. The same pattern applies to a substring or another sequence. section 4.5

Practice with index and slice operations:

  • Return the sums of the even-indexed and odd-indexed numbers in a list or tuple. section 4.5
  • Alternately add and subtract values from a list or tuple. section 4.5
  • Partly emulate zip while accepting indexable inputs. section 4.5

How Python Workout, Second Edition covers it

How deep it goes
Defines indexing and slicing, demonstrates negative slices, and stops short of implementation internals.
Examples
Uses final-element slicing, substring access, index-parity sums, alternating arithmetic, and partial zip emulation.
What you can do after
Apply it via reusable Python functions that index or slice multiple sequence types.
How it connects
Connects indexable types with dynamic typing, sequences, slices, lists, tuples, strings, and zip.

Also explained in

Compare how 2 books explain indexable type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.