Page 181 -
P. 181

comprehending data






              Let’s see what happens to your data when each of Python’s sorting options is used. Start by creating an unordered
              list at the IDLE shell:

              >>> data = [6, 3, 1, 2, 4, 5]    Create a list of
              >>> data                         unordered data and
              [6, 3, 1, 2, 4, 5]               assign to a variable.

              Perform an in-place sort using the sort() method that is built in as standard to every Python list:

              >>> data.sort()         Perform IN-PLACE sorting on the data.
              >>> data
              [1, 2, 3, 4, 5, 6]         The data’s ordering has changed.

              Reset data to its original unordered state, and then perform a copied sort using the sorted() BIF:
              >>> data = [6, 3, 1, 2, 4, 5]
              >>> data                         The data’s ordering has been reset.
              [6, 3, 1, 2, 4, 5]
              >>> data2 = sorted(data)
                                               Perform COPIED sorting on the data.
              >>> data
              [6, 3, 1, 2, 4, 5]             Same as it ever was.
              >>> data2
                                                from lowest to highest.
              [1, 2, 3, 4, 5, 6]                The copied data is ordered





                                                Either sorting option works with the coach’s data, but let’s use a
                                                copied sort for now to arrange to sort the data on output. In the
                                                space below, provide four amended print() statements to
                                                replace those at the bottom of your program.



















                                                                                      you are here 4    145
   176   177   178   179   180   181   182   183   184   185   186