Module 9 - Lists and Tuples Flashcards
What is a sequence? Name two types of sequences Python provides.
An object that contains multiple items of data
The generic term for an ordered set
Lists and tuples are two (of many) types of sequences
What’s an object?
Anything that can contain information: a variable, a file, a string..
What’s the difference between a list and tuple?
A list is mutable and tuple is immutable (not changeable)
What’s a list? What’s an element?
A list is a sequence of values, an object that contains multiple data items
An element is an item in a list
What’s the format/ syntax for a list in Python?
list = [item1, item2, etc.]
T or F: An error will occur if all items in a list are not the same type.
False - a list can hold different types of items
How do you display a list? How do you convert certain objects to lists?
print function to display
list () function to convert to list
How do you make multiple copies of a list?
Repetition operator: makes multiple copies of a list and joins them together
list * n
What is the result of [1,2] * 4?
[2] + [4,7]?
[1,2,1,2,1,2,1,2]
[2,4,7]
How do you iterate over a list? What is the syntax?
you can use a for-loop!
for x in list:
(same way we did strings!)
What’s an index (w.r.t. lists)?
A number specifying the position of an element in a list
What’s the index of the first element? The nth element? The last element?
Index of first element is always 0, nth element has an index of n-1, last element is -1
(can use negative indexes to identify positions relate to the end of the list)
What’s a good way to think about element vs. index numbering?
Think of index as location:
Count from 0!
The first element has a LOCATION (index) of 0
The nth element has a location of n-1
How do you find the length of a list? How do you find index from length?
can use len(list) function
index of the last element is len (list) - 1
What happens if you iterate over a list w/ a loop and use an invalid index?
IndexError
To avoid this, be sure to use len( ) - 1 as the last index iteration
What is an exception?
AKA runtime error
The error does not appear until after the program has started running
(e.g., type code to iterate over a list, but the index is outside the boundary of the list)
How do you assign a new value to an existing list element (e.g., the second element)?
list [1] = new_value
*must be valid index!
T or F: Only lists of the same type can be concatenated.
False (Python 3)
(this was True in earlier versions of Python)
Can do: [1,2] + [‘a’,’e’]
Can also use +=
If you assign: z=x+y, then you change x, what happens to z?
z remains old x+y (NOT the updated x value!)
*IMPORTANT!
What is a slice?
A span of items that are taken from a sequence
What is the list slicing format?
list[start : end]
Span is a list containing copies of elements from start up to, but not including, end
What happens if start or end not specified in a span/slice?
If start not specified, 0 is used for start index
If end not specified, len(list) is used for end index
Can slicing expressions include a step value or negative index?
Yes (negative indexes relative to end of list)
X = [1, 3, 5, 2, 9]
what do the following return?
X [-4:-2]
X [-1:2]
[3,5]
[ ]