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 5 Predict the output of the following programs: Program 1: Python def gfgFunction(): "Geeksforgeeks is cool website for boosting up technical skills" return 1 print (gfgFunction.__doc__[17:21]) Output:coolExplanation: There is a docstring defined for this method, by putting a string
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 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 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5
3 min read
Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read
Output of Python programs | Set 9 (Dictionary) Prerequisite: Dictionary 1) What is the output of the following program? Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.items(): print(key) dictionary.clear(); for key, values in diction
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?Python data = 50 try: data = data/0 except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Division successful ', end = '') try:
3 min read
Output of python program | Set 11(Lists) Pre-requisite: List in python 1) What is the output of the following program? Python data = [2, 3, 9] temp = [[x for x in[data]] for x in range(3)] print (temp) a) [[[2, 3, 9]], [[2, 3, 9]], [[2, 3, 9]]] b) [[2, 3, 9], [2, 3, 9], [2, 3, 9]] c) [[[2, 3, 9]], [[2, 3, 9]]] d) None of these Ans. (a) Exp
3 min read
Output of python program | Set 12(Lists and Tuples) Prerequisite: List and Tuples Note: Output of all these programs is tested on Python3 1) What is the output of the following program? PYTHON L1 = [] L1.append([1, [2, 3], 4]) L1.extend([7, 8, 9]) print(L1[0][1][1] + L1[2]) a) Type Error: can only concatenate list (not "int") to list b) 12 c) 11 d) 3
3 min read
Output of python program | Set 13(Lists and Tuples) Prerequisite: Lists and Tuples1) What is the output of the following program? PYTHON List = [True, 50, 10] List.insert(2, 5) print(List, "Sum is: ", sum(List)) a) [True, 50, 10, 5] Sum is: 66 b) [True, 50, 5, 10] Sum is: 65 c) TypeError: unsupported operand type(s) for +: 'int' and 'str' d) [True, 5
3 min read
Output of python program | Set 14 (Dictionary) Prerequisite: Dictionary Note: Output of all these programs is tested on Python31) 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
3 min read