Lists and tuples
Collect values in order and access them by position.
A list holds values in order. Use square brackets to create one. Index 0 gives the first member and -1 gives the last. Use .append() to add a member or assign to an index to replace one.
A tuple is another ordered collection. It is useful when the set of positions should stay fixed, as with a coordinate. Use unpacking such as x, y = point to give each member a name.
The first slot is at index 0 and the second is at index 1.
Try it yourself
Create a list containing "tea" and "coffee". Append "water", then print the last item. Create a coordinate tuple (2, 8), unpack it, and print the sum.
Target output
water
10Show a hint
Use list[-1] for the last item and two names on the left to unpack a pair.
Ready
Run your code to see the output here.
The first run downloads Python. Code runs in your browser. Progress and drafts are stored on this device.
Show one solution
drinks = ["tea", "coffee"]
drinks.append("water")
print(drinks[-1])
point = (2, 8)
x, y = point
print(x + y)