Professional Documents
Culture Documents
(i)
D+3
(ii)
D*3
(iii)
D + {3 : "3"}
(iv)
(v)
D.update({"3" : 3})
(vi)
D.update("3": 3)
Answer =
Answer =
There should not list as key. If we use tuple in place of list as a key then, it give no error.
Like this: -
d1 = {"a" : 1, 1 : "a", (1, "a") : "two"}
Answer =
Dictionary ‘dict1’ will successfully run and dictionary ‘dict2’ will give error.
It can be corrected by removing list from tuple which is as a key.
So,
Answer =
Because here d1, d2 and d3 are as keys of dictionary d4, but we know that keys should be
immutable data type. But here we provide dictionary as key which is mutable data type.
val = dic1['age']
if val in dic1 :
else :
Answer =
Because ‘in’ condition check only that key is present or not in dictionary. But in given
program, value is passed in ‘in’ condition.
Output: -
dict_keys([5, 'a'])
dict_values([[6, 7, 8], (1, 2, 3)])
>>>
7. 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
Now Output is :-
d
30
False
('d', 30)
>>>
(v) myLst = [('a', 27), ('b', 43), ('c', 25), ('d', 30)]
print ()
Answer =
Output: -
Dictionary contents
5 : number numbernumbernumber
a : string stringstringstring
(1, 2) : tuple tupletupletuple
>>>
9Q. Predict the output:
(i)
d = dict ()
d ['left'] = '<'
d ['right'] = '>'
(ii)
d = dict ()
d ['left'] = '<'
d ['right' ] = '>'
print ("end")
(iii)
text = "abracadabraaabbccrr"
counts = {}
ct = 0
lst = []
counts[word] = 0
ct = ct + 1
(iv)
list1 = [2, 3, 3, 5, 3, 2, 5, 1, 1]
counts = {}
ct = 0
lst = []
counts[num] = 0
ct = ct + 1
Answer =
Output: -
(i)
(ii)
>
>
end
>>>
(iii)
{'a': 1}
['a']
{'a': 1, 'b': 1}
['a', 'b']
{'a': 1, 'b': 1, 'r': 1}
['a', 'b', 'r']
{'a': 2, 'b': 1, 'r': 1}
['a', 'b', 'r']
{'a': 2, 'b': 1, 'r': 1, 'c': 1}
['a', 'b', 'r', 'c']
{'a': 3, 'b': 1, 'r': 1, 'c': 1}
['a', 'b', 'r', 'c']
{'a': 3, 'b': 1, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 1, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 2, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 6, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 3, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 2, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 3, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 4, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
>>>
(iv)
{2: 1}
{2: 2}
{2: 2, 3: 1}
{2: 4, 3: 3}
{2: 4, 3: 4}
{2: 8, 3: 12}
{2: 8, 3: 12, 5: 1}
{2: 16, 3: 36, 5: 5}
{2: 16, 3: 37, 5: 5}
{2: 32, 3: 111, 5: 25}
{2: 33, 3: 111, 5: 25}
{2: 66, 3: 333, 5: 125}
{2: 66, 3: 333, 5: 126}
{2: 132, 3: 999, 5: 630}
{2: 132, 3: 999, 5: 630, 1: 1}
{2: 264, 3: 2997, 5: 3150, 1: 1}
{2: 264, 3: 2997, 5: 3150, 1: 2}
{2: 528, 3: 8991, 5: 15750, 1: 2}
>>>
(h) Delete the item from the dictionary corresponding to the key 9
Answer =
(a) dic.keys()
(b) dic.values()
(c) dic.items()
(d) len( dic )
(e) 7.0 in dic
(f) 2.0 in dic
(g) dic[ 9.0 ]
(h) del dic [ 9.0 ]
(a)
text = "abracadbra"
counts = {}
(b)
my_dict = {}
my_dict[1, 2, 4] = 8
my_dict[[4, 2, 1]] = 10
print (my_dict)
Answer =
(a)
fruit = {}
for index in L1 :
else :
print (len(fruit))
print (fruit)
(b)
arr = {}
arr [1] = 1
arr ['1'] = 2
arr[1] += 1
sum = 0
for k in arr :
print (sum)
Answer =
Output: -
(a)
3
{'Apple': 1, 'banana': 1, 'apple': 1}
>>>
(b)
4
>>>
(a)
a = {(1, 2) : 1, (2, 3) : 2}
print(a[1, 2])
(b)
Answer =
Output: -
(a)
1
>>>
(b)
Error : (
(a)
box = {}
jars = {'Jam' : 4}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
crates['box'] = box
crates['jars'] = jars
print (len(crates[box]))
(b)
box = {}
jars = {'Jam' : 4}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
crates['box'] = box
crates['jars'] = jars
print (len(crates['box']))
Answer =
Output: -
(b)
2
>>>
15 Q. Predict the output:
dct = {}
dct[1] = 1
dct['1'] = 2
dct[1.0] = 4
sum = 0
for k in dct:
Answer =
10
14
6
print(d)
fd = { }
print(fd)
Answer =