Computer Science, asked by prabhakar8812, 2 months ago

Find the error in following cod. State reason of error

aLst={'a':1, 'b':2, 'c':3}
print (aLst['a' ,'b'])​

Answers

Answered by sagarmankoti
2

aLst={'a':1, 'b':2, 'c':3}

print (aLst['a' ,'b'])​

There are 2 error here.

First there shouldn't be a space between print and the parentheses.

Second, 'a','b' is considered as a tuple containing elements 'a' and 'b' hence it will look for a key ('a','b') in the give dictionary aLst which doesn't exist. Hence we get a KeyError.

Answered by ItzImran
14

The above cod.e will produce KeyError, the reason being that there is no key same as the list ['a' , 'b'] in dictionary aLst.

It seems that the above cod.e intends to print the values of two keys 'a' and 'b', thus we can modify the above cod.e to perform this as:

⟹ aLst={'a':1, 'b':2, 'c':3}

⟹print (aLst['a'] , aLst['b']

\fbox\red{Now it will give this result as:}

\green{1\:\:\:2}

Similar questions