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



9 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )


  1. print (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

    ReplyDelete
    Replies
    1. valA = i
      valB = myDict[i]

      It is in for loop so "i" will be the last value of myDict that is d and its value is 30.

      Delete
  2. myLst = ((myDict.items()))
    myLst.sort()# Line 4
    print (myLst[-1])# Line 5"""

    ReplyDelete
  3. what's the output of the above question

    ReplyDelete
  4. Just explain how "d" came in line 1

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post