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
7 Likes
Answer
30
{(1, 2, 4): 8, (4, 2, 1): 10, (1, 2): 12}
Working
- An empty dictionary named
my_dict
is initialized. my_dict[(1,2,4)] = 8, my_dict[(4,2,1)] = 10, my_dict[(1,2)] = 12
these lines assign values to the dictionarymy_dict
with keys as tuples. Since tuples are immutable, so they can be used as keys in the dictionary.- The
for
loop iterates over the keys of the dictionarymy_dict
. Inside the loop, the value associated with each key k is added to the variablesum
. sum
andmy_dict
are printed.
Answered By
2 Likes