Computer Science
Predict the output
a = {'a':1, 'b':2, 'c':3}
print(a['a','b'])
Python Dictionaries
7 Likes
Answer
Output
KeyError: ('a', 'b')  
Explanation
a is a dictionary containing three key-value pairs.a['a','b'] will throw an error since given key ('a','b') is not present in dictionary a.
Answered By
3 Likes
Related Questions
- Predict the output: - arr = {} arr[1] = 1 arr['1'] = 2 arr[1] += 1 sum = 0 for k in arr: sum += arr[k] print(sum)
- Predict the output - a = {(1,2):1,(2,3):2} print(a[1,2])
- Find the error/output. Consider below given two sets of codes. Which one will produce an error? Also, predict the output produced by the correct code. - (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']))
- Predict the output : - dct = {} dct[1] = 1 dct ['1'] = 2 dct[1.0] = 4 sum = 0 for k in dct: print(k, sum) sum += dct[k] print(sum)