← Python Crash Course

PCC·03 · Chapter record

Introducing Lists

Chapter 3 — storing, changing, and organizing sets of data

Done

Five sessions on Python's most useful data structure: creating a list and indexing into it, changing and adding elements, removing them, organizing them, and the classic IndexError mistake. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.

3.1

What Is a List?

  • A list is a collection of items in a particular order, written with square brackets [] and items separated by commas.
  • A list usually holds more than one item, so it's good style to give it a plural name — bicycles, not bicycle.
  • Access one item by its index — the item's position inside square brackets, like bicycles[0].
  • Python starts counting at 0, not 1 — the first item is index 0, the second is index 1, and so on.
  • Index -1 always returns the last item, no matter how long the list is — -2 is the second-to-last, and so on.
list
an ordered collection of items in square brackets
element
a single item stored in a list
index
an item's position in a list, starting at 0
Activity 1

Store a few friends' names in a list, then print each one by indexing it individually (0, 1, 2…).

Activity 2

Using that same list, print a short personalized message to each friend by indexing into the list.

Activity 3

Make a list of your favorite mode of transportation (e.g. car or bike brands), then print a sentence using one indexed item.

▶ List Explorer