02-Jul-2021
.
Admin
Hello Friends,
Now let's see example of how to get smallest element of array in python. I would like to share with you find the smallest element in that given array using python. We will show python get min element in array.
In this article, I will give simple and easy way to get min element of array using 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 = [88, 15, 70, 30, 85]
print("Smallest element is: ", min(arr))
Output
Smallest element is: 15
Example 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))
Output
Smallest element is: 15
Example 3
# Python3 program to find minimum
# in arr[] of size n
# python function to find minimum
# in arr[] of size n
def findSmallest(arr,n):
# Initialize minimum element
min = arr[0]
# Traverse array elements from second
# and compare every element with
# current min
for i in range(1, n):
if arr[i] < min:
min = arr[i]
return min
# Array
arr = [20, 55, 900, 566, 8]
n = len(arr)
smallestElement = findSmallest(arr,n)
print ("Smallest element is",smallestElement)
Output
Smallest element is: 8
It will help you....
#Python