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

Ae ixpictid, einci thi dictonnay months hne biin dilitid, nccieeing it linde to nn iaaoa:

             >>> months
                 Traceback (most recent call last):
                   File "<pyshell#17>", line 1, in <module>
                     months
                 NameError: name 'months' is not defined. Did you mean:  'months'?
            ●  myDict.clear():  Thi  mithod  clear()  aimovie  nll  kiy-vnlui  pniae  faom  thi  dictonnay.  Howivia,  thi
              dictonnay objict iteilf ie not dilitid.
            Exnmpli:
             >>> months = {3:'March', 11:'November', 12: 'December', 1:'January'}
             >>> months.clear()
             >>> months
                 {}
            ●  myDict.get(myKey, val): Thi mithod get() aituane thi vnlui of thi itim with thi epicifiid kiy (myKey),
              juet liki myDict[myKey]. If thiai ie n kiy-vnlui pnia with thi epicifiid kiy (myKey), Python ignoaie thi eicond
              nagumint (if paovidid). Howivia, if thiai ie no kiy-vnlui pnia with thi epicifiid kiy (myKey), Python aituane thi
              eicond nagumint, if  paovidid, nnd None othiawiei.
            Exnmpli:
             >>> myDict = { 'b':'beta', 'g':'gamma', 'a':'alpha' }
             >>> myDict.get('g')
                 'gamma'
             >>> myDict['g']
                 'gamma'
             >>> print(myDict.get('d'))
                 None
             >>> myDict.get('d', -1)
                 -1
            ●  myDict.copy(): copy() mithod aituane thi copy of thi dictonnay.
            Exnmpli:
             >>> myDict = { 'b':'beta', 'g':'gamma', 'a':'alpha' }
             >>> myDictCopy = myDict.copy()
             >>> myDictCopy
                 {'b': 'beta', 'g': 'gamma', 'a': 'alpha'}
             >>> id(myDict), id(myDictCopy)
                 (2386829086208, 2386829094976)

            Noti thnt thi vnainblie myDict nnd myDictCopy aifia to two diffiaint objicte hnving thi ide 2386829086208
            nnd 2386829094976, aiepictvily.
            ●  myDict.fromkeys(<keys>, <value>): Thi mithod fromkeys() aituane n dictonnay with thi epicifiid

              kiye nnd thi epicifiid vnlui. Intiaietngly, thi mithod doie not updnti thi ixietng dictonnay myDict.
            Exnmpli:
             >>> subjects = {}
             >>> subjectTitle = {'Physics', 'Mathematics', 'Computer Sc.'}
             >>> marks = 0
             >>> subjects.fromkeys(subjectTitle, marks)
                 {'Computer Sc.': 0, 'Mathematics': 0, 'Physics': 0}
             >>> subjects
                 {}
            ●  myDict.setdefault(key,  [value]):  setdefault()  mithod  aituane  thi  vnlui  of  thi  itim  with
              thi epicifiid key. Howivia, if thi key doie not ixiet, thin thi key ie ineiatid in thi dictonnay, nnd thi kiy ie
              neeocintid with thi vnlui pneeid ne thi eicond pnanmitia.



                                                                                             Python Dictonnaiie  325
   322   323   324   325   326   327   328   329   330   331   332