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 :
- Display the keys
- Display the values
- Display the items
- Find the length of the dictionary
- Check if 7 is present or not
- Check if 2 is present or not
- Retrieve the value corresponding to the key 9
- 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
Predict the output:
text = "abracadabraaabbccrr" counts = {} ct = 0 lst = [] for word in text: if word not in lst: lst.append(word) counts[word] = 0 ct = ct + 1 counts[word] = counts[word] + 1 print(counts) print(lst)
Predict the output:
list1 = [2, 3, 3, 2, 5,3, 2, 5, 1,1] counts = {} ct = 0 lst = [] for num in list1: if num not in lst: lst.append(num) counts[num] = 0 ct = ct+1 counts[num] = counts[num]+1 print(counts) for key in counts.keys(): counts[key] = key * counts[key] print(counts)
Find the errors:
text = "abracadbra" counts = {} for word in text : counts[word] = counts[word] + 1
my_dict = {} my_dict[(1,2,4)] = 8 my_dict[[4,2,1]] = 10 print(my_dict)