Page 382 - Computer Science Class 11 With Functions
P. 382
Enter state:Manipur
Enter capital:Imphal
Enter state:Tripura
Enter capital:Agartala
Enter state:
Enter capital:
{'Bihar': 'Patna', 'Haryana': 'Chandigarh', 'Jharkhand': 'Ranchi', 'Manipur': 'Imphal',
'Tripura': 'Agartala'}
Enter a state: Haryana
Capital of Haryana is Chandigarh
Enter a capital: Agartala
States with capital Agartala : {'Tripura'}
Let's Summarise
Ø A dictionary is an unordered set of key:value pairs.
Ø A dictionary may be specified by enclosing the key:value airs in braces { }.
Ø An empty pair of braces denotes an empty dictionary.
Ø Dictionary Methods: Python provides several methods for manipulating the dictionaries. These methods are
as follows:
o myDict.keys(): returns a dict_keys object comprising the set of keys included in the dictionary.
o myDict.values(): returns a dict_values object comprising the set of values included in the
dictionary.
o myDict.items(): returns a dict_items object comprising the set of items included in the dictionary.
o myDict. update(): used to add some key-value pairs to an existing dictionary.
o myDict.pop(myKey): removes the item with the specified key.
o myDict.popitem(): removes the last inserted item from the dictionary.
o myDict. clear(): method empties the dictionary.
o myDict.sorted(): used to sort by the keys or by the values.
o myDict.get(myKey): returns the value of the item with the specified key.
o myDict.copy(): returns a copy of the dictionary.
o myDict.fromkeys(keys, value): returns a dictionary with the specified keys and the specified
value.
o myDict. setdefault(key, [value]): returns the value of the item with the specified key.
Ø del statement can also be used to remove key-value pair(s) from a dictionary.
380 Touchpad Computer Science-XI

