KnowledgeBoat Logo
|

Computer Science

Create a dictionary 'ODD' of odd numbers between 1 and 10, where the key is the decimal number and the value is the corresponding number in words.
Perform the following operations on this dictionary :

  1. Display the keys
  2. Display the values
  3. Display the items
  4. Find the length of the dictionary
  5. Check if 7 is present or not
  6. Check if 2 is present or not
  7. Retrieve the value corresponding to the key 9
  8. Delete the item from the dictionary corresponding to the key 9

Python Dictionaries

12 Likes

Answer

>>> ODD = { 1 : 'One', 3 : 'Three', 5 : 'Five', 7 : 'Seven', 9 : 'Nine' }

>>> ODD.keys()
# (1) Display the keys

>>> ODD.values()
# (2) Display the values

>>> ODD.items()
# (3) Display the items

>>> len(ODD)
# (4) Find the length of the dictionary

>>> 7 in ODD
# (5) Check if 7 is present or not 

>>> 2 in ODD
# (6) Check if 2 is present or not 

>>> ODD[9]
# (7) Retrieve the value corresponding to the key 9

>>> del ODD[9]
# (8) Delete the item from the dictionary corresponding to the key 9
Output
dict_keys([1, 3, 5, 7, 9]) 
dict_values(['One', 'Three', 'Five', 'Seven', 'Nine'])  
dict_items([(1, 'One'), (3, 'Three'), (5, 'Five'), (7, 'Seven'), (9, 'Nine')]) 
5
True
False
Nine

Answered By

4 Likes


Related Questions