Page 219 - Information_Practice_Fliipbook_Class11
P. 219

Ø   An empty pair of braces denotes an empty dictionary.
              Ø   The sum() function can be used to add up the values in a dictionary.
              Ø   A nested dictionary is a dictionary that contains one or more dictionaries as values.
              Ø   Dictionary Methods: Python provides several methods for manipulating the dictionaries. These methods are
                  as follows:
                  •  myDict.keys(): returns a dict_keys object comprising the set of keys included in the dictionary.

                  •  myDict.values(): returns a dict_values object comprising the set of values included in the
                     dictionary.

                  •  myDict.items():  returns  a  dict_items  object  comprising  the  set  of  items  included  in  the
                     dictionary.
                  •  myDict.update(): used to add some key-value pairs to an existing dictionary.
                  •  myDict.pop(myKey): removes the item with the specified key.
                  •  myDict.popitem(): removes the last inserted item from the dictionary.

                  •  myDict.clear(): method empties the dictionary.
                  •  myDict.sorted(): used to sort by the keys or by the values.
                  •  myDict.get(myKey): returns the value of the item with the specified key.
                  •  myDict.copy(): returns a copy of the dictionary.
                  •  myDict.fromkeys(keys, value): returns a dictionary with the specified keys and the specified
                     value.

                  •  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.





                                                  Solved Exercises


            A.  Multiple Choice Questions
               1.  Which of the following statements will delete a key-value pair for the key 'tue' in the following dictionary?
                 days = {'sun':1,'mon':2,'tue':3,'wed':4,'thur':5}
                 a. delete days['tue']                            b.  del.days['tue']
                 c.  del days['tue']                              d.  days.del['tue']
               2.  Which of the following will display the keys in the dictionary myDict?
                 a.  myDict.keys()                                b.  keys(myDict)
                 c.  myDict.key()                                 d.  print(myDict.key())
               3.  Consider the following dictionary:
                 numSquares = {1:1, 2:4, 3:9, 4:16, 5:25}
                  Which of the following will delete the key:value pair 2:4?
                 a.  del numSquares[2:4]                          b.  del numSquares[2]
                 c.  numSquares.remove(2)                         d.  numSquares.remove(2:4)
              4.  Which of the following will remove all the elements of the dictionary numSquares, but not delete the dictionary object
                  itself?
                 a.  clear numSquares                             b.  del numSquares
                 c.  numSquares.clear()                           d. clear(numSquares)


                                                                                             Python Dictionaries  205
   214   215   216   217   218   219   220   221   222   223   224