What will be the result of the following code?
d1=("abc":5,def":6,"ghi":7)
Print [d1(0)]
Answers
The result of the following còde will be an Error
Explanation:
Before we start with the explanation of the answer let us correct the question as it is incorrect.
What will be the result of the following còde?
d1={"abc":5,"def":6,"ghi":7}
print(d1[0])
A. abc
B. 5
C. {"abc":5}
D. Error
D. Error is the correct answer.
d1 is a dictionary that has keys abc, def, and ghi and its values are 5, 6, and 7 respectively.
In the print statement, it is written d1[0] and there is no key in the dictionary that has 0 as key.
When we run this còde in the compiler it gives Key Error.
The còde given in the question is written in Python programming language.
d1={"abc":5,"def":6,"ghi":7}
print(d1["abc"])
print(d1["def"])
print(d1["ghi"])
print(d1["ab"])
After running this còde in compiler
Output:
5
6
7
Key Error: "ab"
ERRORs.
Since this is a Python program.
In the first line, a dictionary is created but there is a mistake there.
d1 = { "abc":5,"def":6,"ghi":7 }
In the next line,
Since the dictionary has its own key and value pairs. So, it can not be called through any indexing.
print(d1 [ "abc" ] )
The errors are highlighted and underlined.