Write a Python program to find the largest and smallest element in an array:
a=[4,6,2,8,1,3,9,2,5,4,8,0,2,3,5,7,5,2]
a.sort()
print("The largest element is",a[-1])
print("The smallest element is",a[0])
Output :
The largest element is 9
The smallest element is 0
The sort()
method is called on the list a
, which rearranges the elements in ascending order. The list a
will be modified to [0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 7, 8, 8, 9]
.
a[-1]
refers to the last element in the list, which is the largest element in the sorted list.
a[0]
refers to the first element in the list, which is the smallest element in the sorted list.
If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).
HAPPY LEARNING !!!