Page 72 - Digicode_AI_class_8
P. 72
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 will start by finding the smallest number 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.
Now you will follow a similar technique for the rest of the numbers. You will find the smallest number
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.
70 DigiCode AI-VIII

