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

7.1.6 Dictionary (Mapping)

            A dictionary (dict) is an unordered set of key-value pairs enclosed in curly brackets:{}. It maps a set of keys to a set
            of values. The key is separated from its value using a colon(:) and key-value pairs are separated from each other by
            commas (,). A dictionary does not allow repeated keys.

             >>> furniturePrice  = {'chair':500, 'table': 2000, 'stool':1000}
             >>> furniturePrice
                 {'chair': 500, 'table': 2000, 'stool': 1000}
             >>> furniturePrice['table']
                 2000

                     For each of the following, identify its type.
                     1.  14.5
                     2.  -203
                     3.  14+7j
                     4.  True
                     5.  "Python"
                     6.  [34,'Book', 25]
                     7.  ('Small','Medium','Large')
                     8.  {'Small','Medium','Large'}
                     9.  {'rollno':10,'name':'Joy'}


            7.2 Mutable and Immutable DataTypes

            Data types in Python are broadly categorised into two types—mutable and immutable. A modifiable object and its
            associated type are called mutable. For example, lists and dictionaries are mutable. Numeric data types (int, float,
            and complex), bool, str, and tuple, are immutable data types (see Fig 7.2). Next, we give examples of mutable
            and immutable data objects.
             >>> num1 = 50
             >>> id(num1)
                 1734046084944
            Recall that the function id() yields the object id of an object. Fig. 7.2 shows object 50 being referenced by the
            variable num1.



                                   num1                           50
                                                                                 1734046084944

                                              Fig 7.2: num1 pointing to the data object 50
            Next, consider another assignment statement:
             >>> num2 = num1
             >>> id(num2)
                 1734046084944
            Note that each of the variables num1, and num2 refers to the same object, 50 in the memory.

                                      num1                         50
                                                                             1734046084944



                                      num2


                                         Fig 7.3: num1 and num2 pointing to the same object 50

                                                                                        Data Types and Operators  151
   148   149   150   151   152   153   154   155   156   157   158