Q. Consider the following code and then answer the questions that follow:
myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30}
valA = ''
for i in myDict :
if i > valA :
valA = i
valB = myDict[i]
print (valA) # Line 1
print (valB) # Line 2
print (30 in myDict) # Line 3
myLst = list((myDict.items()))
myLst.sort() # Line 4
print (myLst[-1]) # Line 5
Answer =
(i) d
(ii) 30
(iii) False
(iv) error
(v) error
Here Line 4 produce Error because myLst is not list.
myLst = dict_items([('a', 27), ('b', 43), ('c', 25), ('d', 30)])
its data type is not List.
So, Correct code :-
myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30} valA = '' for i in myDict : if i > valA : valA = i valB = myDict[i] print (valA)# Line 1 print (valB)# Line 2 print (30 in myDict)# Line 3 myLst = ((myDict.items())) myLst.sort()# Line 4 print (myLst[-1])# Line 5"""
Now Output is :-
d
30
False
('d', 30)
>>>
So, Correct answer of (iv) and (v) is :-
(iv) ('d', 30)
(v) myLst = [('a', 27), ('b', 43), ('c', 25), ('d', 30)]
Data Type = List
wrong answer
ReplyDeleteNo. It is correct question is wrong.
DeletePls check your answers
ReplyDeleteOk, I have Explain it.
Deleteprint (valA) # Line 1
print (valB) # Line 2
print (30 in myDict) # Line 3
may i know how u got the input as
d
30
false
valA = i
DeletevalB = myDict[i]
It is in for loop so "i" will be the last value of myDict that is d and its value is 30.
myLst = ((myDict.items()))
ReplyDeletemyLst.sort()# Line 4
print (myLst[-1])# Line 5"""
what's the output of the above question
ReplyDeleteJust explain how "d" came in line 1
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )