Page 65 - Computer Genius Class 07
P. 65

To add an item in an array:
                 Append (append() is a method to add an element to an array) every item to the array.
                 a=[ ]

                 for I in range(11)
                 m.append(i)
                 print(m)

                 To remove an item from an array:
                 pop() is a method to remove an element from an array.
                 Colors=[“red”, “green”, “blue”]
                 Colors.pop(1)

                 print(Colors)

                 Sorting a List

                 Sorting is arranging the numbers either in ascending order (smallest number to largest number) or
                 descending order (largest number to smallest number).
                 Suppose you have a list of unsorted numbers and you want to sort the list in ascending order. The
                 condition to sort the list is that you don’t have any extra space to store the number; you have to
                 manage the space given.
                 To sort the list  you  ill start  y finding the smallest num er present in the list. Once you find the
                 smallest number, you will put it at the top of the list. But where will you place the number which is
                 already present at the top of the list? You will simply swap or interchange the number present at the
                 top of the unsorted list with the smallest number.

                  o  you  ill  ollo  a similar techni ue  or the rest o  the num ers.  ou  ill find the smallest num er
                 from the remaining unsorted list and swap it with the number present. You need to repeat this
                 process till you reach the last number from the list.
                 When you reach the last element from the list you need to check that all the numbers from the list
                 are arranged in ascending order. This method of sorting is called ‘Selection Sort’.
                 Algorithm:

                 Step 1    Set Minimum number to location 0

                 Step 2    Search for the minimum element in the list
                 Step 3    Swap the number at location Minimum number

                 Step 4    Increment the Minimum number to point it to the next element

                 Step 5    Repeat the above step 1 to 4, till the list get sorted





                     Guido Van Rossum developed Python, one of the most popular coding languages
                                         used by programmers around the world.





                                                                                Advanced MakeCode Arcade      63
   60   61   62   63   64   65   66   67   68   69   70