Page 372 - Computer Science Class 11 With Functions
P. 372

>>> myDict = {}  # Create an empty dictionary
         >>> myDict['a'] = 'alpha'
         >>> myDict['b'] = 'beta'
         >>> myDict['g'] = 'gamma'
         >>> myDict
              {'a': 'alpha', 'b': 'beta', 'g': 'gamma'}
        In the above example, the first statement creates an empty dictionary, and the subsequent assignment statements add
        key-value pairs to the dictionary. The Greek letter corresponding to the key 'b' may be retrieved as follows:

         >>> myDict['b']
              'beta'
        The expression dict() also creates an empty dictionary, as shown below:

         >>> dictionary = dict()  # Create an empty dictionary
        14.2  Aggregate  operations  min,  max,  and sum  that  can be  Applied on
        Dictionaries

        Given a dict object, you can use min, max, and sum on its set of keys. For example, consider the dict object
        months that maps month numbers to their names:

         >>>  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
        Note that the expression  min(months)  yields  the  least  value  of    the  key  in  the  dictionary  months. Similarly,
        the  expression  max(months)  yields  the  maximum  value  of    the  key  in  the  dictionary  months.  The  function
        len(months) yields the number of key-value pairs in a dictionary. For example,

         >>> len(months)
              12
        The sum() function can be used to add up the values in a dictionary. However, using the sum() function directly on
        a dictionary will only add up the keys of the dictionary, not the values.
        For example,

         >>> myDict = {'a': 10, 'b': 20, 'c': 30}
         >>> sumKeys = sum(myDict)
         >>> print(sumKeys)
              'abc'
        In this example, the sum() function is called on the dictionary myDict. However, instead of adding up the values of
        the dictionary, it adds up the keys of the dictionary ('a', 'b', 'c'). This results in the output of 'abc'.
        To add up the values of a dictionary using the sum() function, you can use the values() method to get a list of the
        dictionary's values and then pass that list to the sum() function.
        For example,
         >>> myDict = {'a': 10, 'b': 20, 'c': 30}
         >>> sumValues = sum(myDict.values())
         >>> print(sumValues)
              60
        In this example, the values() method is called on the dictionary myDict to get a list of its values. This list ([10,
        20, 30]) is then passed to the sum() function, which adds up the values and returns the sum of values (60).

        14.3 Nested Dictionary

        Python also supports a nested dictionary. A nested dictionary is a dictionary that contains one or more dictionaries as
        values. Each of the inner dictionaries can contain its own set of keys and values.

         370   Touchpad Computer Science-XI
   367   368   369   370   371   372   373   374   375   376   377