Output of python program | Set 14 (Dictionary)
Last Updated :
12 Oct, 2022
Prerequisite: Dictionary
Note: Output of all these programs is tested on Python3
1) What is the output of the following program?
PYTHON3
D = dict ()
for x in enumerate ( range ( 2 )):
D[x[ 0 ]] = x[ 1 ]
D[x[ 1 ] + 7 ] = x[ 0 ]
print (D)
|
a) KeyError
b) {0: 1, 7: 0, 1: 1, 8: 0}
c) {0: 0, 7: 0, 1: 1, 8: 1}
d) {1: 1, 7: 2, 0: 1, 8: 1}
Ans. (c)
Explanation: enumerate() will return a tuple, the loop will have x = (0, 0), (1, 1). Thus D[0] = 0, D[1] = 1, D[0 + 7] = D[7] = 0 and D[1 + 7] = D[8] = 1.
Note: Dictionary is unordered, so the sequence of the key-value pair may differ in each output.
2) What is the output of the following program?
PYTHON3
D = { 1 : 1 , 2 : '2' , '1' : 2 , '2' : 3 }
D[ '1' ] = 2
print (D[D[D[ str (D[ 1 ])]]])
|
a) 2
b) 3
c) ‘2’
d) KeyError
Ans. (b)
Explanation: Simple key-value pair is used recursively, D[1] = 1, str(1) = ‘1’. So, D[str(D[1])] = D[‘1’] = 2, D[2] = ‘2’ and D[‘2’] = 3.
3) What is the output of the following program?
PYTHON3
D = { 1 : { 'A' : { 1 : "A" }, 2 : "B" }, 3 : "C" , 'B' : "D" , "D" : 'E' }
print (D[D[D[ 1 ][ 2 ]]], end = " " )
print (D[D[ 1 ][ "A" ][ 2 ]])
|
a) D C
b) E B
c) D B
d) E KeyError
Ans. (d)
Explanation: Key-Value Indexing is used in the example above. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][2] = “B”, D[D[1][2]] = D[“B”] = “D” and D[“D”] = “E”. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][“A”] = {1 : “A”} and D[1][“A”][2] doesn’t exists, thus KeyError.
4) What is the output of the following program?
PYTHON3
D = dict ()
for i in range ( 3 ):
for j in range ( 2 ):
D[i] = j
print (D)
|
a) {0: 0, 1: 0, 2: 0}
b) {0: 1, 1: 1, 2: 1}
c) {0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
d) TypeError: Immutable object
Ans. (b)
Explanation: 1st loop will give 3 values to i 0, 1 and 2. In the empty dictionary, values are added and overwritten in j loop, for eg. D[0] = [0] becomes D[0] = 1, due to overwriting.
5) Which of the options below could possibly be the output of the following program?
PYTHON3
D = { 1 : [ 1 , 2 , 3 ], 2 : ( 4 , 6 , 8 )}
D[ 1 ].append( 4 )
print (D[ 1 ], end = " " )
L = [D[ 2 ]]
L.append( 10 )
D[ 2 ] = tuple (L)
print (D[ 2 ])
|
a) [1, 2, 3, 4] [4, 6, 8, 10]
b) [1, 2, 3, 4] ((4, 6, 8), 10)
c) ‘[1, 2, 3, 4] TypeError: tuples are immutable
d) [1, 2, 3, 4] (4, 6, 8, 10)
Ans. (b)
Explanation: In the first part key-value indexing is used and 4 is appended into the list. As tuples are immutable, in the second part the tuple is converted into a list, and value 10 is added finally to the new list ‘L’ then converted back to a tuple.
Similar Reads
Output of Python Program | Set 1
Predict the output of following python programs: Program 1: [GFGTABS] Python r = lambda q: q * 2 s = lambda q: q * 3 x = 2 x = r(x) x = s(x) x = r(x) print (x) [/GFGTABS]Output: 24Explanation : In the above program r and s are lambda functions or anonymous functions and q is the argument to both of
3 min read
Output of python program | Set 2
Difficulty level : IntermediatePredict the output of following Python Programs. Program 1: C/C++ Code class Acc: def __init__(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) Output: 111 Explanation: Instantiation of the class "Acc" automatically calls the method __init__ and passes th
2 min read
Output of Python Program | Set 3
Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: class Geeks: def __init__(self, id): self.id = id manager = Geeks(100) manager.__dict__['life'] = 49 print (manager.life + len(manager.__dict__)) Output:51 Explanation : In the above program we are creating a
2 min read
Output of Python Program | Set 4
Difficulty level : Intermediate Predict the output of the following Python Programs. Program 1: nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] print nameList[1][-1] Output: k Explanation: The index position -1 represents either the last element in a list or the last character in a String. In the abo
2 min read
Output of Python program | Set 5
Predict the output of the following programs: Program 1: [GFGTABS] Python def gfgFunction(): "Geeksforgeeks is cool website for boosting up technical skills" return 1 print (gfgFunction.__doc__[17:21]) [/GFGTABS]Output: coolExplanation: There is a docstring defined for this method,
3 min read
Output of Python program | Set 6 (Lists)
Prerequisite - Lists in Python Predict the output of the following Python programs. These question set will make you conversant with List Concepts in Python programming language. Program 1 Python Code list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0
3 min read
Output of Python programs | Set 7
Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1[GFGTABS] Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ",
3 min read
Output of Python programs | Set 8
Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 [GFGTABS] Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) [/GFGTABS]Output: 6Explanation: The beauty of python list datatype is that within
3 min read
Output of Python programs | Set 9 (Dictionary)
Prerequisite: Dictionary 1) What is the output of the following program? [GFGTABS] Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.
3 min read
Output of Python programs | Set 10 (Exception Handling)
Pre-requisite: Exception Handling in PythonNote: All the programs run on python version 3 and above. 1) What is the output of the following program? [GFGTABS] Python data = 50 try: data = data/0 except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Divisio
3 min read