1.2.21 · D1Introduction to Programming (Python)

Foundations — Lists — creation, indexing, slicing, mutability

1,787 words8 min readBack to topic

Before you touch indexing or slicing, every tiny symbol the parent note used must be built from nothing. Below, each item gives you its plain meaning → the picture → why the topic needs it, ordered so each one leans only on the ones above it.


1. A value and a name (the very first symbols)

Figure — Lists — creation, indexing, slicing, mutability

Look at the figure: the name a is just a tag on a string leading to a box in memory. Nothing about the box is stored inside the name a — the name only knows the address of the box. Keep this picture; the aliasing trap b = a is impossible to misread once you see two tags on one box.

Why the topic needs this: the whole "mutability" story (b = a shares, a[:] copies) is only a story about how many tags point at how many boxes.


2. The square brackets [ ] (making a list)

Why the topic needs this: this is the container itself. Everything else — indexing, slicing, mutability — is a thing you do to this row of boxes.


3. Counting numbers, and where they start

Figure — Lists — creation, indexing, slicing, mutability

In the figure, the forward numbers 0,1,2,3 sit under each box (how far you walked), and the backward numbers -1,-2,-3,-4 sit above (how far back from the end). This one picture is the seed of the negative-index identity the parent proves.

Why the topic needs this: "why does indexing start at 0" and "what does a[-1] mean" are both answered by this single offset picture.


4. The letter n — length of the list

Why the topic needs this: the IndexError case (a[4] on a length-4 list) and the negative-index rule are both stated in terms of .


5. The colon : — cutting between boxes (slicing)

Figure — Lists — creation, indexing, slicing, mutability

Look at the figure: cutting at gap 2 and gap 5 gives you exactly boxes 2,3,4 — three boxes, and . That is why stop is excluded and why the slice length is simply : you are just measuring the distance between two fence-posts.

Why the topic needs this: the "why is stop excluded", the clean length formula, and the perfect join of a[:k] with a[k:] all fall out of this one gap picture.


6. The three types you'll see inside a list

Why the topic needs this: the parent's mixed = [1, "hi", 3.5, True] and nested = [[1,2],[3,4]] examples rely on exactly this "box holds an address" fact.


7. The dot . — asking a list to do something

Why the topic needs this: the classic trap a = a.append(x) (which sets a = None) is only understandable once you know .append returns None and = re-tags.


How these feed the topic

value and name

equals as re-tag

b = a shares one box

square brackets make a list

row of boxes

index as offset from 0

negative index n minus k

length n

valid range and IndexError

colon cuts between gaps

slice length stop minus start

dot calls a method

append mutates returns None

Lists indexing slicing mutability

Every arrow above points at a claim in the parent note. If any source box confuses you, re-read its section above before continuing to the parent topic.


Quick self-checks


Equipment checklist

You are ready for the parent topic when you can answer each without peeking:

What does = actually do in Python?
Evaluates the right side, then ties (re-tags) the left-side name to that value — it is an action, not an equality claim.
Draw what a = [10,20,30] looks like in memory.
A name a tagged to a row of three boxes holding 10, 20, 30 (the name stores the box's address).
Why is the first index 0?
An index is an offset — distance from the start — and the first box is 0 steps in.
For length n, what is the last valid forward index?
n - 1.
Convert index -k to a positive index.
n - k (so -1 is n-1, the last box).
Why is stop excluded in a[start:stop]?
Slice numbers mark the gaps between boxes; cutting at gaps start and stop leaves the length as stop - start.
Length of the slice a[2:5]?
5 - 2 = 3 boxes.
Why can a list mix ints, strings and other lists?
Each box stores only an address, so it does not care what kind of value sits at that address.
What does the dot in a.append(x) mean?
The list a owns the append action; apply it with argument x.
What does a.append(x) return?
None — it edits the list in place rather than producing a new list.