Page 57 - Touchcode_C8_Flipbook
P. 57
1 2 3 4 5 6 7
Python has an inbuilt sort function (sort( ): to arrange the list in ascending order) to sort an
array.
Example: numbers=[7,2,9,4]
numbers.sort()
Print(numbers)
Result:
[2,4,7,9]
SEARCHING IN AN ARRAY
To search an element in an array, Python uses the indexing method.
Consider an array:
X=[“R”, “D”, “F”, “H”]
Print(X.index(“F”))
If you run the above code, you would get ‘2’ as output.
The index method works on numeric arrays too:
x=[1,6,5,3,4]
Print(x.index(5))
If you run the above code, you would get ‘2’ as output.
Coding uiz 02 Critical Thinking
Fill in the blanks.
a. Array improves the ........................... of code.
b. The variables in an array are ordered ........................... .
USING PYTHON TO ARRANGE AN ARRAY IN
ASCENDING ORDER USING BUBBLE SORT IN MAKECODE
Bubble Sort is the simplest sorting algorithm which works by repeatedly swapping the
adjacent elements if they are in the wrong order.
Programming with Arrays 55

