Python 3.1Code walkthrough9 parts

Lists

Store ordered, changeable collections and slice them.

Loading your progress…Next: 3.2 Tuples

In this lesson9 parts

  1. 01The list we already have
  2. 02Indexing, and negative indices
  3. 03Slicing
  4. 04Changing a list: append, insert, remove and pop
  5. 05Asking questions of a list: len, sum, min, max, in, index and count
  6. 06sort and sorted
  7. 07Two names, one list: aliasing
  8. 08Copies, and rebinding against mutation
  9. 09Recap, your challenge, and tuples next

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

alias
A second name bound to the same object: after backup = cart, backup is an alias of cart.
element
One value stored in a list.
index
An element's position, counting from zero. A negative index counts from the end, from -1.
method
A function that belongs to an object, called with a dot: cart.append(...), review.lower().
mutable
A type whose objects can be changed after they are created; a list is mutable, and so are sets and dictionaries.
mutation
Changing an object in place, which every alias sees: append, remove, sort, assigning to an index.
slice
A range of elements, from a start up to but not including a stop, with an optional step: temps[2:5], temps[::-1]. A slice is a new list.

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 1 · Lists and Tuples.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.