liveBook · Manning

Indexable type

As explained in Python Workout: 50 ten-minute exercises

An indexable type is a type whose values support selecting an element or subsequence by a numeric index or slice.

An indexable type lets a function accept values that support either a single numeric index or a slice, making one implementation useful across related data types. section 3.5

This pattern favors general functions over type-specific duplicates: one function can find the largest element in a string, list, or tuple. Slices also select subsequences, such as the final three elements with mylist[-3:]. section 3.5 section 3.5

Practice index and slice operations with:

  • A function returning the sums at even and odd indexes, such as [90, 120] for [10, 20, 30, 40, 50, 60]. section 3.5
  • A function that alternately adds and subtracts numbers, producing 50 from 10+20-30+40-50+60. section 3.5
  • A partial implementation of the built-in zip function for multiple inputs. section 3.5

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Defines indexing and slicing, explains their reuse, and stops at exercise-level applications rather than implementation internals.
Examples
Uses mylist[-3:], even-odd sums, alternating arithmetic, and a partial zip implementation.
What you can do after
Apply indexes and slices to write functions that handle multiple compatible data types.
How it connects
Connects dynamic typing with variables, sequences, slices, and generic functions.

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: 50 ten-minute exercises. The section links point to the parts of the book it draws on.