Class - 12 CBSE Computer Science Important Output Questions 2025
What will be the output of the following code snippet ?
my_dict = {}
my_dict[(1,2,4)] = 8
my_dict[(4,2,1)] = 10
my_dict[(1,2)] = 12
sum = 0
for k in my_dict:
sum += my_dict[k]
print(sum)
print(my_dict)
Python
Python Dictionaries
10 Likes
Answer
30
{(1, 2, 4): 8, (4, 2, 1): 10, (1, 2): 12}
Working
- An empty dictionary named
my_dictis initialized. my_dict[(1,2,4)] = 8, my_dict[(4,2,1)] = 10, my_dict[(1,2)] = 12these lines assign values to the dictionarymy_dictwith keys as tuples. Since tuples are immutable, so they can be used as keys in the dictionary.- The
forloop iterates over the keys of the dictionarymy_dict. Inside the loop, the value associated with each key k is added to the variablesum. sumandmy_dictare printed.
Answered By
3 Likes