Lists and Dictionaries

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Complete the code using formatting specifiers to generate the described output. Assume price = 150. *I saved $150!* print('I saved $_________!' % price)

%d

Complete the code using formatting specifiers to generate the described output. Assume percent = 40. *Buy now! Save 40%!* print('Buy now! Save __________!' % percent)

%d%%

Complete the code using formatting specifiers to generate the described output. Use the indicated variables. Assume item = 'backpack' and weight = 5.2. *The backpack is 5.200000 pounds.* print('The %s is ________ pounds.' % (item, weight))

%f

Complete the code using formatting specifiers to generate the described output. Use the indicated variables. Assume item = 'burrito' and price = 5. *The burrito is $5* print('The ________ is $%d' % (item, price))

%s

What is the value of sys.argv[1] given the following command-line input (include quotes in your answer): *python prog.py 'Tricia Miller' 26*

'Tricia Miller'

What is the value of sys.argv[1] given the following command-line input (include quotes in your answer): *python prog.py Tricia Miller 26*

'Tricia'

Complete the print statement to produce the given output using mapping keys. Use single quotes for strings. "I need 12 lilies, 6 roses, and 18 tulips." print ('I need %(lilies)d lilies, %(roses)d roses, and %(tulips)d tulips.' % {_______________________________________________})

'lilies': 12, 'roses': 6, 'tulips': 18

Complete the print statement to produce the given output using mapping keys. Use single quotes for strings. "My name is Jerome and I'm 15 years old." print ('My name is %(name)s and I am %(age)d years old' % {_______________________________________________})

'name': 'Jerome', 'age': 15

Create a nested list nums whose only element is the list [21, 22, 23].

*nums* = [[21, 22, 23]]

Assume that my_list is [0, 5, 10, 15]. What value is returned by *min*(my_list)?

0

Count how many odd numbers (cnt_odd) there are. for i in num: if i % 2 == 1: cnt_odd += 1

0

Given the list nums = [[10, 20, 30], [98, 99]], what does nums[*0*][*0*] evaluate to?

10

Consider the following program: 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

Assume that my_list is [0, 5, 10, 15]. What value is returned by *max*(my_list)?

15

Determine the output of each code segment. If the code produces an error, type None. Assume that my_dict has the following entries: *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

Assume the following list has been created: 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

Determine the output of each code segment. If the code produces an error, type None. Assume that my_dict has the following entries: *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

Assume that 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

Given the list nums = [[10, 20, 30], [98, 99]], what does nums[*1*][*1*] evaluate to?

99

All elements of a list must have the *same* type. True or False

False

Assume that my_list is [0, 5, 10, 15]. What value is returned by *all*(my_list)?

False

Dictionaries can contain, at most, three levels of nesting. True or False

False

Given the following code: nums = [0, 25, 50, 75, 100] The result of evaluating nums[0:5:2] is [25, 75]. True or False

False

The output of print(sorted([-5, 5, 2])) is [2, -5, 5]. True or False

False

The output of the following is [13, 7, 5]: *primes = [5, 13, 7]* *primes.sort()* *print(primes)* True or False

False

The size of a list is determined when the list is created and *cannot* change. True or False

False

The statement *del my_list[2]* produces a new list without the element in position 2. True or False

False

The variable my_dict created with the following code contains two keys, 'Bob' and 'A+'. my_dict = dict(name = 'Bob', grade = 'A+') True or False

False

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

A program *can* modify the elements of an existing list. True or False

True

A programmer can iterate over a copy of a list to safely make changes to that list. True or False

True

Assume that my_list is [0, 5, 10, 15]. What value is returned by *any*(my_list)?

True

Dictionary entries can be modified in-place - a new dictionary does not need to be created every time an element is added, changed, or removed. True or False

True

Given the following code: nums = [0, 25, 50, 75, 100] The result of evaluating nums[0:-1:3] is [0, 75]. True or False

True

Iterating over a list and deleting elements from the original list might cause a logic program error. True or False

True

Nested dictionaries are a flexible way to organize data. True or False

True

The expression {'D1': {'D2': 'x'}} is valid. True or False

True

The sort() method modifies a list in-place. True or False

True

The statement *my_list1 + my_list2* produces a *new* list. True or False

True

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. Only negative *odd* values from the list x numbers = ___________________________________________________

[(i) for i in x if ((i % 2 == 1) and (i < 0))]

Assume that the following code has been evaluated: nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums[1:5]?

[1, 2, 3, 5]

Given the list nums = [[10, 20, 30], [98, 99]], what does nums[*0*] evaluate to?

[10, 20, 30]

What's the output of the list comprehension program in row 4 of the table above if my_list is [[5, 10], [1]]?

[15, 1]

Assume that the following code has been evaluated: nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums [3:-1]?

[3, 5, 8]

What's 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]

Assume that the following code has been evaluated: nums = [1, 1, 2, 3, 5, 8, 13] What is the result of nums[5:10]?

[8, 13]

Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable. The absolute value of each element in x. Use the abs() function to find the absolute value of a number.

[abs(i) for i in x]

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. Only *negative* values from the list x numbers = __________________________________________________

[i for i in x if i < 0]

Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable. Twice the value of each element in the list variable x.

[i*2 for i in x]

Given the following code, what this output of each code segment? animals = ['cat', 'dog', 'bird', 'raptor'] print(animals[*2*])

bird

Given the following code, what this output of each code segment? animals = ['cat', 'dog', 'bird', 'raptor'] print(animals[*0*])

cat

Count how many negative numbers (cnt_neg) there are. cnt_neg = 0 for i in num: if i < 0:

cnt_neg += 1

Complete the code using formatting specifiers to generate the described output. Use the indicated variables. Assume city = 'Boston' and distance = 2100. *We are 2100 miles from Boston.* print('We are %d miles from %s.' % (_____________________))

distance, city

Given the following code, what this output of each code segment? animals = ['cat', 'dog', 'bird', 'raptor'] print(animals[*0 + 1*])

dog

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 = [____________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 ________________: div_ten += 1

i % 10 == 0

Write a list comprehension that contains elements with the desired values. Use the name 'i' as the loop variable. The largest square root of any element in x. Use math.sqrt() to calculate the square root.

max([math.sqrt(i) for i in x])

Fill in the code, using the dict methods items(), keys(), or values() where appropriate. Change all negative values in my_dict to 0. for key, value in __________________: if value < 0: my_dict[key] = 0

my_dict.items()

Fill in the code, using the dict methods items(), keys(), or values() where appropriate. Print each key in the dictionary my_dict. for key in _________________: print(key)

my_dict.keys()

Fill in the code, using the dict methods items(), keys(), or values() where appropriate. Print twice the value of every value in my_dict. for v in ________________: 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()

Given the following code, what this output of each code segment? animals = ['cat', 'dog', 'bird', 'raptor'] i = 3 print(animals[*i*])

raptor

Assume the following list has been created: 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 = __________([sum(row) for *row* in *my_list*]) print(sum_list)

sum

Provide an expression using x.sort that sorts the list x accordingly. Arrange the elements of x from lowest to highest, comparing the upper-case variant of each element in the list.

x.sort(key = str.upper)

Provide an expression using x.sort that sorts the list x accordingly. Sort the elements of x such that the greatest element is in position 0.

x.sort(reverse = True)


संबंधित स्टडी सेट्स

6th Ref Exam (Civil Engineering) Part 1

View Set

Amortized mortgage and balloon payments

View Set

Intro to Psychology: Chapter 7 Mid Term Review

View Set

EDF 435 Study Set Chapters 1 & 2

View Set

business law for entrepreneurs ch 6

View Set

Management principles chpt 10,12,14

View Set

World Religions- Christianity & Islam

View Set

Principles of Microeconomics CH.1 Quiz

View Set