Dictionaries Output#
Find the output of the following code.
Please don’t run the code before giving your answer.
Question-1#
dict_exp = {'table':1, 'chair':2, 'washer':3}
for i in dict_exp.keys():
print(i[-2])
Solution
l
i
e
Question-2#
dict_exp = {'table':[1,5,9], 'chair':[10,20,30], 'washer':[2,7,4]}
total = 0
for i in dict_exp.values():
total += i[1]
print(total)
Solution
32
Question-3#
dict_exp = {'table':[1,5,9], 'chair':[4,6,8], 'washer':[2,7,4]}
total = 0
for i in dict_exp.values():
for j in i:
total += i[1]
print(total)
Solution
54
Question-4#
dict_exp = {'table':[1,5,9], 'chair':[4,6,8], 'washer':[2,7,4]}
total = 0
for i in dict_exp.values():
for j in i:
total += j
print(total)
Solution
46
Question-5#
stock_dict = {'day-1':{'High': 70, 'Low':62, 'Close':65},
'day-2':{'High': 68, 'Low':60, 'Close':63},
'day-3':{'High': 71, 'Low':65, 'Close':67},
'day-4':{'High': 70, 'Low':62, 'Close':65},
'day-5':{'High': 73, 'Low':65, 'Close':70},
'day-6':{'High': 75, 'Low':69, 'Close':73}
}
for i in [1,3]:
print(stock_dict['day-'+str(i)]['Low'])
Solution
62
65
Question-6#
stock_dict = {'day-1':{'High': 70, 'Low':62, 'Close':65},
'day-2':{'High': 68, 'Low':60, 'Close':63},
'day-3':{'High': 71, 'Low':65, 'Close':67},
'day-4':{'High': 70, 'Low':62, 'Close':65},
'day-5':{'High': 73, 'Low':65, 'Close':70},
'day-6':{'High': 75, 'Low':69, 'Close':73}
}
total = 0
for i,j in stock_dict.items():
total += int(i[-1])
print(total)
Solution
21
Question-7#
stock_dict = {'day-1':{'High': 70, 'Low':62, 'Close':65},
'day-2':{'High': 68, 'Low':60, 'Close':63},
'day-3':{'High': 71, 'Low':65, 'Close':67},
'day-4':{'High': 70, 'Low':62, 'Close':65},
'day-5':{'High': 73, 'Low':65, 'Close':70},
'day-6':{'High': 75, 'Low':69, 'Close':73}
}
total = 0
for i,j in stock_dict.items():
total += j['High']//10
print(total)
Solution
41
Question-8#
my_dict = {200:'NY', 100:'NJ', 400:'TX', 300:'CA'}
for i in my_dict.keys():
if i>188:
print(my_dict[i][1])
Solution
Y
X
A
Question-9#
mydict = {'A':(10,100), 'B':(20, 200), 'C':(30,300)}
for i,j in mydict.values():
print(i,j)
Solution
10 100
20 200
30 300
Question-10#
grades = {'Tim':90, 'Amy':95, 'Jack':85}
print(grades.keys())
Solution
dict_keys([‘Tim’, ‘Amy’, ‘Jack’])
Question-11#
grades = {'Tim':90, 'Amy':95, 'Jack':85}
print(grades.values())
Solution
dict_values([90, 95, 85])
Question-12#
grades = {'Tim':90, 'Amy':95, 'Jack':85, 'Tim':50}
grades['Tim']
Solution
50
Question-13#
grades = {'Tim':90, 'Amy':95, 'Jack':85}
print(80 in grades)
Solution
False
Question-14#
grades = {'Tim':90, 'Amy':95, 'Jack':85}
print('Jack' in grades)
Solution
True
Question-15#
grades = {'Tim':90, 'Amy':95, 'Jack':85}
print(len(grades))
Solution
3
Question-16#
grades = {'Tim':90, 'Amy':95, 'Jack':85}
grades['Amy'] = 100
print(grades)
Solution
{‘Tim’: 90, ‘Amy’: 100, ‘Jack’: 85}