Chapter 3
Lists and tuples
Lists keep items in order, and you can add, remove and sort them. Tuples look similar, in round brackets, but never change once made. This chapter covers append() and pop(), slicing, checking membership with in, sorting in place, unpacking a tuple into variables, and two classic surprises: reading past the end of a list, and two names that point at the very same list.
11 cards
See the 11 cards
- What does append() leave in the list?[1, 2, 3, 4]
- What does len() print for a list?3
- What does a print after changing b?[1, 2, 3]
- What does the slice [1:3] of the list print?[20, 30]
- What does pop() return and leave behind?3 [1, 2]
- What does the in operator print here?True
- What's wrong here: reading item 2?IndexError
- What's wrong here: changing a tuple?TypeError
- Fill the blank to put the list in ordersort
- What does tuple unpacking put in y?8
- Brackets that make a tupleRound brackets
Image credits
- What does append() leave in the list?: Brain Zap, CC0 1.0 Source
- What does len() print for a list?: Brain Zap, CC0 1.0 Source
- What does a print after changing b?: Brain Zap, CC0 1.0 Source
- What does the slice [1:3] of the list print?: Brain Zap, CC0 1.0 Source
- What does pop() return and leave behind?: Brain Zap, CC0 1.0 Source
- What does the in operator print here?: Brain Zap, CC0 1.0 Source
- What's wrong here: reading item 2?: Brain Zap, CC0 1.0 Source
- What's wrong here: changing a tuple?: Brain Zap, CC0 1.0 Source
- Fill the blank to put the list in order: Brain Zap, CC0 1.0 Source
- What does tuple unpacking put in y?: Brain Zap, CC0 1.0 Source