ch 11 practice questions Flashcards
What will print?
t1 = (‘a’,)
print(type(t1))
t2 = (‘a’)
print(type(t2))
- tuple
- str
Do you need a comma for an empty tuple?
no
Which of these lines of code correctly creates a tuple?
a. t = ()
b. t = tuple()
c. t = tup()
d. t = ‘a’, ‘b’, ‘c’, ‘d’
a, b , d
Can you modify a tuple? what can you do instead?
no, you cannot modify a tuple
you can replace one tuple with another
What does the following print?
t = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’)
t = (‘A’,) + t[1:]
print(t)
(‘A’, ‘b’, ‘c’, ‘d’, ‘e’)
example of replacing instead of modifying
What is returned? Why?
(5, 7, 3) < (7, 4, 2)
True, only compares the first values and ignores the rest (because the first one is true)
how does sort() work for a list of tuples?
sort() compares the first elements of a tuple, if they tie, it moves onto the second
What does DSU stand for?
Decorate: building a list of tuples with one or more sort metrics preceding the elements from the sequence
Sort: sort
Undecorate: extracting the sorted elements of the sequence
Identify DSU in the following Code:
txt = ‘but soft what light in yonder window breaks’
words = txt.split()
tupleList = []
for word in words:
tupleList.append((len(word), word))
tupleList.sort(reverse = True)
sortedWordsList = []
for length, word in tupleList:
sortedWordsList.append(word)
print(sortedWordsList)
decorate would be adding the len(word) and word in tupleList
sort is the use of the sort function
Undecorate:
sortedWordsList = []
for length, word in tupleList:
sortedWordsList.append(word)
What is associated with the variable ‘domain’ once the following code is run?
email = yeezy@hotmail.com
uname, domain = email.split(‘@’)
hotmail.com
what is the dictionary method .items()?
returns a list of tuples, where each tuple is a key-value pair.
what does the following print?
d = {‘a’:10, ‘b’:1, ‘c’:22}
t = list(d.items())
print(t)
[(‘a’, 10), (‘b’, 1), (‘c’, 22)]
How will the list below be sorted (if there is any order at all)?
grocery_dict = {‘apple’: 5, ‘pineapple’: 3, ‘chicken’: 8, ‘kiwi’: 7}
grocery_list = list(grocery_dict.items())
grocery_list.sort()
ascending order by key’s first letter
How will the contents of list “lst” be ordered after the following code is run?
d = {‘a’: 10, ‘b’: 15, ‘c’: 17, ‘d’: 4}
lst = []
for key, val in d.items():
lst.append( (val, key) )
print(lst)
no order b/c it’s hash key order
How will the following code be sorted once this code is run?
weather = {‘Reykjavik’: 60, ‘Buenos Aires’: 55, ‘Cairo’: 96, ‘Berlin’: 89, ‘Caloocan’: 78}
sorted_weather = weather.sort()
error, you can’t sort dictionaries
Which of these options can be keys of a dictionary? Select all that apply.
a) dictionaries
b) strings
c) tuples
d) integers
e) lists
B, C, D
A(n) ________ is an error that is caused when a data structure has the wrong type, size, or composition
shape error