Page 434 - Computer Science V2.0 Class 11
P. 434

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.





                                                    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)
               420   Touchpad Computer Science (Ver. 2.0)-XI
   429   430   431   432   433   434   435   436   437   438   439