Output of Python Programs | Set 24 (Dictionary)

Prerequisite : Python-Dictionary

1. What will be the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

dictionary = {"geek":10, "for":45, "geeks": 90}
print("geek" in dictionary)

chevron_right


Options:



  1. 10
  2. False
  3. True
  4. Error
Output:
3. True

Explanation: in is used to check the key exist in dictionary or not.

2. What will be the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

dictionary ={1:"geek", 2:"for", 3:"geeks"}
del dictionary

chevron_right


Options:

  1. del deletes the entire dictionary
  2. del doesn’t exist for the dictionary
  3. del deletes the keys in the dictionary
  4. del doesn’t exist for dictionary
Output:
1. del deletes the entire dictionary

Explanation: del deletes the entire dictionary and any further attempt to access it will throw an error.

3. What will be the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

a = {}
a[1] = 1
a['1'] = 2
a[1]= a[1]+1
count = 0
for i in a:
    count += a[i]
print(count)

chevron_right


Options:

  1. 4
  2. 2
  3. 1
  4. Error
Output:
1. 4

Explanation: The above piece of code basically finds the sum of the values of keys.

4. What will be the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

test = {1:'A', 2:'B', 3:'C'}
del test[1]
test[1] = 'D'
del test[2]
print(len(test))

chevron_right


Options:

  1. 2
  2. 1
  3. 0
  4. Error
Output:
1. 2

Explanation: After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.

5. What will be the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

a ={}
a['a']= 1
a['b']=[2, 3, 4]
print(a)

chevron_right


Options:

  1. {‘b’: [2], ‘a’: 1}
  2. {‘b’: [2, 3, 4], ‘a’: 1}
  3. {‘b’: [2], ‘a’: [3]}
  4. Error
Output:
2. {‘b’: [2, 3, 4], ‘a’: 1}

Explanation: Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.