Math and Python: subscripts are indices
Subscripts are indices
Here we make explicit the connection between subscript notation in mathematics and indices in Python.
In mathematics: Say we have a collection of objects X. We can refer to individual elements of the collection by associating each element of the collection with some index from the natural numbers. Thus,
\begin{align*} x_0 &\in X \\ x_1 &\in X \\ &\ldots \\ x_n &\in X \\ \end{align*}
Different texts may use different starting indices. For example, a linear algebra text probably starts indices at one. A text on set theory is likely to use indices starting at zero.
In Python, sequences—lists, tuples, and strings—are indexed in this fashion. All Python indices start at zero, and we refer to Python as being zero indexed.
Indexing works the same for lists, tuples, and even strings. Remember that these are sequences—ordered collections—so each element has an index, and we may access elements within the sequence by its index.
= ['P', 'O', 'R', 'C', 'U', 'P', 'I', 'N', 'E'] my_list
We start indices at zero, and for a list of length n, the indices range from zero to n - 1.
It’s exactly the same for tuples.
= ('P', 'O', 'R', 'C', 'U', 'P', 'I', 'N', 'E') my_tuple
The picture looks the same, doesn’t it? That’s because it is! It’s even the same for strings.
= 'PORCUPINE' my_string
While we don’t explicitly separate the characters of a string with commas, they are a sequence nonetheless, and we can read characters by index.
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.