COSC 1306 - SET 4: Lists and Dictionaries
Assume price = 150 print('I saved $ --blank-- !' % price) Complete the code using formatting specifiers to generate the described output
%d
Assume percent = 40 print('Buy now! Save --blank-- !' % percent) Complete the code using formatting specifiers to generate the described output
%d%%
Assume item = 'backpack' and weight = 5.2 print('The %s is --blank-- pounds.' & (item, weight))
%f
Assume item = 'burrito' and price = 5 print('The -- blank-- is $%d' % (item, price))
%s
What is the value of sys.argv[1] given the following command-line input: python prog.py 'Tricia Miller' 26 (include quotes in your answer)
'Tricia Miller'
What is the value of sys.argv[1] given the following command-line input: python prog.py Tricia Miller 26 (include quotes in your answer)
'Tricia'
"I need 12 lilies, 6 roses, and 18 tulips." print(I need %(lilies)d lilies, %(roses)d roses, and %(tulips)d tulips.' % { ---blank---})
'lilies': 12, 'roses': 6, 'tulips': 18
"My name is Jerome and I'm 15 years old." print('My name is %(name)s and I am %(age)d years old' % { ---blank---})
'name': 'Jerome', 'age': 15
Count how many odd numbers (cnt_odd) there are. cnt_odd = __blank__ for i in num: if i % 2 == 1: cnt_odd += 1
0
my_list is [0, 5, 10, 15] What value is returned by min(my_list)?
0
nums = [[10, 20, 30], [98, 99]] What does nums[0][0] evaluate to?
10 nums[0] returns the list [10, 20, 30]. The 1st element of that list, nums[0][0], is 10.
nums = [10, 20, 30, 40, 50] for pos, value in enumerate(nums): tmp = value / 2 if (tmp % 2) == 0: nums[pos] = tmp What's the final value of nums[1]?
10.0
my_list is [0, 5, 10, 15] What value is returned by max(my_list)?
15
my_dict = dict(bananas=1.59, fries=2.39, burger=3.50, sandwich=2.99) my_dict['burger'] = my_dict['sandwich'] val = my_dict.pop('sandwich') print(my_dict['burger'])
2.99 removing a key only removes that key and any shared reference to a value still exists
scores = [ [75, 100, 82, 76], [85, 98, 89, 99], [75, 82, 85, 5] ] How many elements does scores contain? (The result of len(scores))
3
my_dict = dict(bananas=1.59, fries=2.39, burger=3.50, sandwich=2.99) my_dict.update(dict(soda=1.49, burger=3.69)) burger_price = my_dict.get('burger', 0) print(burger_price)
3.69
my_list is [0, 5, 10, 15] What value is returned by sum(my_list)?
30
What is the output of the following program? temps = [65, 67, 72, 75] temps.append(77) print(temps[-1])
77
nums = [[10, 20, 30], [98, 99]] What does nums[1][1] evaluate to?
99 nums[1] returns the list [98, 99]. The 2nd element of that list, nums[1][1], is 99.
The size of a list is determined when the list is created and cannot change.
False Lists can grow and shrink as necessary.
All elements of a list must have the same type.
False Lists can have heterogeneous types of elements.
The variable my_dict created with the following code contains two keys, 'Bob' and 'A+' my_dict = dict(name='Bob', grade='A+')
False The code creates a new dict containing the keys name and grade.
Dictionaries can contain, at most, three levels of nesting.
False The level of nesting is arbitrary.
my_list is [0, 5, 10, 15] What value is returned by all(my_list)?
False The list contains 0, thus all() returns False because not all elements are non-zero
nums = [0, 25, 50, 75, 100] The result of evaluating nums[0:5:2] is [25, 75]
False The start and end positions cover all the elements of the list. However, the stride is 2, so every other element, starting from 0 is extracted. Thus, the result is [0, 50, 100].
primes = [5, 13, 7] primes.sort() print(primes) The output of the following is [13, 7, 5]:
False list.sort() arranges the items from lowest to highest
The output of 'print(sorted([-5, 5, 2])) is [2, -5, 5]
False sorted() sorts elements of the given list argument and returns a new list, sorted from smallest to largest value
The statement 'del my_list[2]' produces a new list without the element in position 2.
False. 'del' performs in-place modification, returning the same list without deleted element.
What is the output of the following program? actors = ['Pitt', 'Damon'] actors.insert(1, 'Affleck') print(actors[0], actors[1], actors[2])
Pitt Affleck Damon Position 1 is the list initially is 'Damon'. The inserted value is placed at the position 1, and 'Damon' is moved to position 2.
A programmer can iterate over a copy of a list to safely make changes to that list.
True
Iterating over a list and deleting elements from the original list might cause a logic program error.
True
Nested dictionaries are a flexible way to organize data.
True
The sort() method modifies a list in-place
True
my_list is [0, 5, 10, 15] What value is returned by any(my_list)?
True
Dictionary entries can be modifies in-place - a new dictionary does not need to be created every times an element is added, changed, or removed.
True Dictionaries are mutable, just like lists
nums = [0, 25, 50, 75, 100] The result of evaluating nums[0:-1:3] is [0, 75]
True Every third element is extracted, starting at 0 and not including the last element of the list.
The statement my_list + my_list2 produces a new list.
True List concatenation appends the right operand to the left operand and creates a new list with the result.
A program can modify the elements of an existing list.
True Lists are mutable and elements can be added, removed, or changed as necessary.
The expression {'D1': {'D2': 'x'}} is valid
True The expression creates a nested dictionary with the key 'D1', containing the key D2 with the value 'x'.
Only negative odd values from the list x numbers = --blank-- Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable. Use parentheses around the expression or condition as necessary.
[(i) for i in x if ((i < 0) and (i % 2 == 1))]
my_list = [1, 1, 2, 3, 5, 8, 13, 21, 34] What is the result of my_list[len(my_list//2 : (len(my_list)//2 + 1)
[1, 1, 2, 3, 5, 8, 13, 21, 34]
nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums[1:5]?
[1, 2, 3, 5]
nums = [[10, 20, 30], [98, 99]] What does nums[0] evaluate to?
[10, 20, 30] nums[0] returns the list [10, 20, 30]
Whats the output of the list comprehension program in row 4 of the table above if my_list is [[5, 10], [1]]?
[15, 1]
my_list = [1, 1, 2, 3, 5, 8, 13, 21, 34] What is the result of my_list[2:5]
[2, 3, 5]
my_list = [1, 1, 2, 3, 5, 8, 13, 21, 34] What is the result of my_list[3:6]
[3, 5, 8]
nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums[3:-1]?
[3, 5, 8]
Whats the output of the list comprehension program from row 3 in the table above if the user enters "4 6 100"?
[4, 6, 100]
What's the output of the list comprehension program in row 1 in the table above if my_list is [-5, -4, -3]?
[5, 6, 7]
my_list = [1, 1, 2, 3, 5, 8, 13, 21, 34] What is the result of my_list[4:]
[5, 8, 13, 21, 34]
my_list = [1, 1, 2, 3, 5, 8, 13, 21, 34] What is the result of my_list[:20]
[5]
nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums[5: 10]?
[8, 13]
my_list = [1, 1, 2, 3, 5, 8, 13, 21, 34] What is the result of my_list[3:1]
[] empty set
The absolute value of each element in x. Use abs() function to find the absolute value of a number Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable.
[abs(i) for i in x]
Only negative values from the list x numbers = --blank-- Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable. Use parentheses around the expression or condition as necessary.
[i for i in x if i < 0]
Twice the value of each element in the list variable x Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable.
[i*2 for i in x]
animals = ['cat', 'dog', 'bird', 'raptor'] print(animals[0]) print(animals[2])
bird
animals = ['cat', 'dog', 'bird', 'raptor'] print(animals[0]) print(animals[0])
cat
Count how many negative numbers (cnt_neg) there are. cnt_neg = 0 for i in num: if i < 0: __blank__
cnt_neg += 1
Assume city = 'Boston' and distance = 2100 print('We are %d miles from %s.' % (---blank---))
distance, city
animals = ['cat', 'dog', 'bird', 'raptor'] print(animals[0]) print(animals[0 + 1]
dog 0 + 1 = 1
Alter the list comprehension from row 2 in the table above to convert each number to a float instead of a string. my_list = [5, 20, 50] my_list =[--blank-- for i in my_list] print(my_list)
float(i)
Determine the number of elements in the list that are divisible by 10. (Hint: the number x is divisible by 10 if x % 10 is 0.) div_ten = 0 for i in num: if __blank__: div_ten += 1
i % 10 == 0
The largest square root of any element in x. Use math.sqrt() to calculate the square root. Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable.
max([math.sqrt(i) for i in x])
Change all negative values in my_dict to 0. for key, values in --blank--: if value < 0: my_dict[key] = 0
my_dict.items()
Print each key in the dictionary my_dict for key in --blank--: print(key)
my_dict.keys() or my_dict
Print twice the value of every value in my_dict. for v in --blank--: print(2 * v)
my_dict.values()
Write a statement that counts the number of elements of my_list that have the value 15.
my_list.count(15)
Write the simplest two statements that first sort my_list, then remove the largest value element from the list, using list methods.
my_list.sort() my_list.pop() sort() arranges the elements from smallest to largest pop() removes the last element
Create a nested list nums whose only element is the list [21, 22, 23]
nums = [[21, 22, 23]]
animals = ['cat', 'dog', 'bird', 'raptor'] print(animals[0]) i = 3 print(animals[i])
raptor
scores = [ [75, 100, 82, 76], [85, 98, 89, 99], [75, 82, 85, 5] ] Write an indexing expression that gets the element from scores whose value is 100.
scores[0][1]
Alter the list comprehension from row 5 in the table above to calculate the sum of every number contained by my_list my_list = [[5, 10, 15], [2, 3, 16], [100]] sum_list = --blank--([sum(row) for row in my_list]) print(sum_list)
sum
Arrange the elements of z from lowest to highest, comparing the upper-case variant of each element in the list.
x.sort(key = str.upper)
Sort the elements of x such that the greatest element is in position 0.
x.sort(reverse = True)