Page 188 -
P. 188

hashes and databases


           Associate a key with a value using a hash


                                                        Note the use of curly brackets here.
           Start with an empty hash:

                                       scores = {}
            An empty hash is assigned to a
            variable called “scores".

                                                 Scores




                                                                       The values go here.
                        The keys go here.









          You add data to an existing hash by describing the association between the key
           and the value. Here’s how to associate a surfers’ name with their score:



              Put the key inside the                              ...and put the value to the right of
              square brackets...   scores[8.45] = 'Joseph'        the assignment operator.







                                              Scores
                                                     Joseph
                                       8.45                           Note how the act of assigning a
                                                                      value to a key CREATES the hash
           A new row of data is added to                              entry (assuming it's not already
           the hash. Note the association.                            there). In Python, there's no
                                                                      explicit “append()" method for
                                                                      hashes as there is for arrays.










                                                                                       you are here 4    153
   183   184   185   186   187   188   189   190   191   192   193