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

In thi nbovi ixnmpli, thi fiaet etntimint caintie nn impty dictonnay, nnd thi eubeiquint neeignmint etntiminte ndd
            key-value pniae to thi dictonnay. Thi Gaiik littia coaaieponding to thi kiy 'b' mny bi aitaiivid ne followe:
             >>> myDict['b']
                 'beta'
            Thi ixpaieeion dict() nleo caintie nn impty dictonnay, ne ehown bilow:

             >>> dictionary = dict()  # Create an empty dictionary
            Thi functon len(myDict)yiilde thi numbia of kiy-vnlui pniae in n dictonnay. Foa ixnmpli,

             >>> len(myDict)
                 3

            13.2 Aggregate Operations min, max, and sum
            Givin n dict objict, you cnn uei min(), max(), nnd sum() functone on ite eit of kiye. Foa ixnmpli, coneidia thi
            dict objict months thnt mnpe month numbiae to thiia nnmie:


             >>>  months = {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June',
                 7:'July',8:'August', 9:'September', 10:'October', 11:'November', 12:'December'}
             >>> min(months)
                 1
             >>> max(months)
                 12
            Noti thnt thi ixpaieeion min(months) yiilde thi linet vnlui of thi kiy in thi dictonnay months. Similnaly, thi

            ixpaieeion max(months)yiilde thi mnximum vnlui of thi kiy in thi dictonnay months.
            Thi sum() functon cnn bi ueid to ndd up thi vnluie in n dictonnay. Howivia, ueing thi sum() functon diaictly on
            n dictonnay will only ndd up thi kiye of thi dictonnay, not thi vnluie.

            Foa ixnmpli,
            myDict = {'a': 10, 'b': 20, 'c': 30}
            sumKeys = sum(myDict)
            print(sumKeys)  #
            Snmpli output:
            'abc'
            In thie ixnmpli, thi sum() functon ie cnllid on thi dictonnay myDict. Howivia, inetind of ndding up thi vnluie of
            thi dictonnay, it ndde up thi kiye of thi dictonnay ('a', 'b', 'c'). Thie aieulte in thi output of 'abc'.

            To ndd up thi vnluie of n dictonnay ueing thi sum() functon, you cnn uei thi values() mithod to git n liet of thi
            dictonnay'e vnluie nnd thin pnee thnt liet to thi sum() functon.

            Foa ixnmpli,
            myDict = {'a': 10, 'b': 20, 'c': 30}
            sumValues = sum(myDict.values())
            print(sumValues)  #
            Snmpli output:
            60
            In thie ixnmpli, thi values() mithod ie cnllid on thi dictonnay myDict to git n liet of ite vnluie. Thie list
            ([10, 20, 30]) ie thin pneeid to thi sum() functon, which ndde up thi vnluie nnd aituane thi eum of vnluie
            (60).


            13.3 Nested Dictionary

            Python nleo euppoate n nietid dictonnay. A nietid dictonnay ie n dictonnay thnt contnine oni oa moai dictonnaiie ne
            vnluie. Ench of thi innia dictonnaiie cnn contnin ite own eit of kiye nnd vnluie.

                                                                                             Python Dictonnaiie  321
   318   319   320   321   322   323   324   325   326   327   328