How to Find Smallest Element in An Array in Python?
Published On: 04/06/2022 | Category:
Python
Hi Dev,
This is a short guide on the python program to find the smallest element in an array. I explained simply about find the min value in array python. you'll learn to find min and max values in array python. We will look at an example of finding the minimum element in an array using a function in python.
Here i will give you three example for how to get min element of array in python.
So let's see the below example:
Example 1 :#Initialize array arr = [20, 17, 35, 65, 85] print("Min element is: ", min(arr))Output
Min element is: 17Example 2 :
#Initialize array arr = [88, 15, 70, 30, 85] #Initialize min with first element of array. min = arr[0] #Loop through the array for i in range(0, len(arr)): #Compare elements of array with min if(arr[i] < min): min = arr[i] print("Smallest element is: " + str(min))
Min element is: 15Example 3 :
# python function to find minimum # in arr[] of size n def findSmallest(arr,n): # Initialize minimum element min = arr[0] for i in range(1, n): if arr[i] < min: min = arr[i] return min # Array arr = [444, 555, 111, 222, 333] n = len(arr) smallestElement = findSmallest(arr,n) print ("Min element is",smallestElement)Output
Min element is 111
It will help you....