Create lists, read and slice them by index, and grow or shrink them with append, insert and remove.
01
Learning objectives
Create a list and read or change any element using positive and negative indexes.
Extract a sub-list with slicing and predict exactly which elements a slice returns.
Grow a list with append() and insert(), and shrink it with remove() and pop().
Compute len(), sum(), min() and max() over a list of numbers.
Trace a loop that builds a list step by step and state the final list.
02
Lesson notes
A single variable holds one value; real data rarely comes singly. A class test produces thirty marks, a shopping trip produces a dozen prices, and a cricket innings produces a scorecard of numbers. A list stores many values in one named container, kept in order: marks = [88, 74, 91]. The order matters, because marks[0] is 88 and marks[1] is 74, and the list grows or shrinks while your program runs. Lists are the first data structure every Python programmer meets, and the one the board exam tests most heavily.
Every element lives at a numbered position called an index, and Python counts from zero, not one. In marks = [88, 74, 91, 66, 79], marks[0] is 88 and marks[4] is 79; asking for marks[5] raises an IndexError. Negative indexes count from the end, so marks[-1] is the last element and marks[-2] the second last — a shortcut examiners expect you to know. Because lists are mutable, you can also write marks[1] = 80 and replace a value in place, something strings never allow.
A slice copies out a section: marks[1:4] gives [74, 91, 66] — it starts at index 1 and stops before index 4, so the element at the stop position is never included. This stop-excluded rule confuses everyone at first, but it hides a gift: the length of marks[1:4] is simply 4 − 1 = 3. marks[:3] means from the beginning, marks[3:] means to the end, and marks[:] copies the whole list. Slices never raise errors for out-of-range stops — they just stop early.
Lists come with methods that change them. append(x) adds x at the end — the everyday way to build a list inside a loop. insert(i, x) squeezes x in at position i, shifting everything to the right. remove(x) deletes the first element equal to x (by value, not by position), and pop() removes and returns the last element so you can consume a list item by item. All four work in place: the list object itself changes, and no assignment is needed.
Four built-ins summarise any numeric list instantly: len() counts elements, sum() totals them, and min() and max() pick the extremes — so the class average is sum(marks) / len(marks). Board exam tip: a favourite short question shows three append, insert and remove calls and asks for the final list. Trace them in order on paper, one line per step, and re-check the count with len() after each line — most lost marks come from miscounting after an insert.
List indexing starts at zero
03
Worked code examples
Indexes, negative indexes and the big four built-ins
Indexes count from 0, and the slice [1:4] stops before index 4, so only three elements come out. The five marks add up to 398, with 66 the smallest and 91 the largest.