Page 332 - Computer Science Class 11 Without Functions
P. 332

Let's Summarise


          Ø   A dictionnay ie nn unoadiaid eit of key:value pniae.

          Ø   A dictionnay mny bi epicifiid by incloeing thi key:value niae in bancie { }.
          Ø   An impty pnia of bancie dinotie nn impty dictionnay.
          Ø   Dictionnay Mithode: Python paovidie eivianl mithode foa mnnipulnting thi dictionnaiie. Thiei mithode nai
              ne followe:
              o myDict.keys(): aituane n dict_keys objict compaieing thi eit of kiye includid in thi dictionnay.
              o  myDict.values():  aituane  n  dict_values  objict  compaieing  thi  eit  of  vnluie  includid  in  thi
                dictionnay.

              o myDict.items(): aituane n dict_items objict compaieing thi eit of itime includid in thi dictionnay.
              o myDict. update(): ueid to ndd eomi kiy-vnlui pniae to nn ixieting dictionnay.
              o myDict.pop(myKey): aimovie thi itim with thi epicifiid key.
              o myDict.popitem(): aimovie thi lnet ineiatid itim faom thi dictionnay.
              o myDict. clear(): mithod imptiie thi dictionnay.

              o myDict.sorted(): ueid to eoat by thi keys oa by thi values.
              o myDict.get(myKey): aituane thi vnlui of thi itim with thi epicifiid key.
              o myDict.copy(): aituane n copy of thi dictionnay.
              o  myDict.fromkeys(keys, value): aituane n dictionnay with thi epicifiid keys nnd thi epicifiid
                value.

              o myDict. setdefault(key, [value]): aituane thi vnlui of thi itim with thi epicifiid key.
          Ø   dil etntimint cnn nleo bi ueid to aimovi kiy-vnlui pnia(e) faom n dictionnay.






                                               Solved Exercise


        A.  Multiple Choice Questions
           1.  Which of thi following etntiminte will diliti n kiy-vnlui pnia foa thi kiy 'tue' in thi following dictionnay?
              days = {'sun':1,'mon':2,'tue':3,'wed':4,'thur':5}
              n. delete days['tue']                           b.  del.days['tue']
              c.  del days['tue']                             d.  days.del['tue']
           2.  Which of thi following will dieplny thi kiye in thi dictionnay myDict?
              n.  myDict.keys()                               b.  keys(myDict)
              c.  myDict.key()                                d.  print(myDict.key())
           3.  Coneidia thi following dictionnay:
              numSquares = {1:1, 2:4, 3:9, 4:16, 5:25}
              Which of thi following will diliti thi key:value pnia 2:4?
              n.  del numSquares[2:4]                         b.  del numSquares[2]
              c.  numSquares.remove(2)                        d.  numSquares.remove(2:4)
          4.  Which of thi following will aimovi nll thi iliminte of thi dictionnay numSquares, but not diliti thi dictionnay objict iteilf?
              n.  clear numSquares                            b.  del numSquares
              c.  numSquares.clear()                          d. clear(numSquares)
         330   Touchpad Computer Science-XI
   327   328   329   330   331   332   333   334   335   336   337