Chapter 5 Flashcards
List
-Sequence of data values (items or elements)
-Allows programmer to manipulate a sequence of data values of any types
-Examples: to-do list, shopping list, text document (list of lines)
Dictionary
-Organizes data values by association with other data values rather than by sequential position
-In Python, associates a set of keys with values
-example: the keys in Webster’s dictionary comprise the set of words while the associated data values are their definitions.
-other examples: phone books, address books, encyclopedias etc
Each item in a list has a unique ___ that specifies its position (from____)
-index
-0 to length -1
Is a list mutable or immutable?
mutable
-elements can be inserted, removed, or replaced
A list maintains its ___ but its ___ (its length and contents) can change
-identity
-state
How can a subscript operator be used in a list?
-To replace an element
ie. example[3] = 0
Here “0” replaces the element in position 3 of the list called example
How would you replace each number in a list with its square?
IN: numbers = [2, 3, 4, 5]
IN: for index in range(len(numbers)):
numbers[index] = numbers[index] **2
IN: numbers
OUT: [4, 9, 16, 25]
What method can be used to extract a list of the words in a sentence?
string method split
ie.
IN: sentence = “This example has five words.”
IN: words = sentence.split()
IN: words
OUT: [‘This’, ‘example’, ‘has’, ‘five’, ‘words.’]
How would you convert a list of words to uppercase?
IN: for index in range(len(words)):
words[index] = words[index].upper()
L.append(element)
adds element to the end of L.
L.extend(aList)
Adds the elements of aList to the end of L.
L.insert(index, element)
Inserts element at index if index is less than the length of L. Otherwise, inserts element at the end of L.
L.pop()
Removes and returns the element at the end of L.
L.pop(index)
Removes and returns the element at index
in…
determines an element’s presence or absence, but does not return position of element (if found)
What method that is used with strings can’t be used to locate an element’s position in a list?
the find method
If we can’t use the find method how can we search a list?
-use method index to locate an element’s position in a list.
Since method index raises an exception when the target element is not found how can we guard against this?
-First, use the in operator to test for presence
-then the index method if this test returns True.
ie.
aList = [34, 45, 67]
target = 45
if target in aList:
print(aList.index(target))
else:
print(-1)
natural ordering
the placement of data items relative to each other by some internal criteria, such as numeric value or alphabetical value.
A list’s elements are always ordered by ____ but you can impose a ____ on them
-position
-natural ordering
T or F: When the elements can by related by comparing them <, >, and == they cannot be sorted
F: When the elements can by related by comparing them <, >, and == they can be sorted
Method sort
mutates a list by arranging its elements in ascending order
ie.
IN: l = [4, 2, 10, 8]
IN: l.sort()
IN: l
OUT: [2, 4, 8, 10]
All functions and methods from previous chapters return a value that the caller can then use to complete its work. What kind of methods usually return no value of interest to caller?
mutator methods (e.g., insert, append, extend, pop, and sort)
Since mutator methods usually return no value of interest to caller what special value does Python automatically return?
-None
ie:
IN: aList = aList.sort()
IN: print(aList)
OUT: None