Chapter 10 - Lists and Tuples Flashcards
How are lists similar to strings?
- lists are sequences, and therefore support indexing and slicing (like strings).
- lists are objects, and contain methods which you can call (just as strings do)
How are lists different from strings?
- lists may contain different data types.
- lists are mutable, meaning they can be altered after they are created.
How do we access a item in a list?
indexing and slicing.
How do we add an item to a list?
append
ex. x.append(19)
How do we delete an item from a list?
del
ex. del z[1]
What is the general syntax for selecting items with a list comprehension?
[x for x in sequence if expression]
Where sequence is any sequence and expression is an expression involving x. Each item x from sequence is selected and added to the resulting list if the expression involving x evaluates to true.
What is the general syntax for computing something using each item in a list?
What are the differences between a list and a tuple?
- tuples are immutable.
- tuples are written with parenthesis instead of square brackets.
In what type of situations would we want to use a tuple instead of a list?
Software engineering because tuples prevent accidental modification.