Python Program to Find Minimum and Maximum Value in an Array

Write a Python Program to Find Minimum and Maximum Value in an Array. The numpy module has min and max functions to return the minimum and maximum values in a numpy array. We use these numpy min and max functions to return the minimum and maximum values in the number and string array. 

# Smallest and Largest Array Item

import numpy as np

smtlgtarr = np.array([14, 120, 50, 11, 65, 99, 920,8, 195, 120])
print("Numeric Numpy Array Items = ", smtlgtarr)
print("The Smallest Number in this Numpy Array = ", min(smtlgtarr))
print("The Largest Number in this Numpy Array  = ", max(smtlgtarr))

strsmtlgtarr = np.array(['UK', 'Brazil', 'USA','India', 'Japan'])
print("String Numpy Array Items = ", strsmtlgtarr)
print("The Smallest Number in this Numpy Array = ", min(strsmtlgtarr))
print("The Largest Number in this Numpy Array  = ", max(strsmtlgtarr))
Python Program to Find Minimum and Maximum Vlaue in an Array

Python Program to Find Minimum and Maximum Value in an Array

We used the numpy sort function to sort the numpy array in ascending order. Next, we print the number at the first and last index position, which Arte minimum and maximum array values.

# Largest and Smallest Array Item

import numpy as np

smtlgtarr = np.array([99, 120, 50, 9, 428, 16, 190])
print("Numpy Array Items = ", smtlgtarr)

smtlgtarr.sort()
lgtlength = len(smtlgtarr) - 1

print("The Smallest Number in smtlgtarr Numpy Array = ", smtlgtarr[0])
print("The Largest Number in smtlgtarr Numpy Array = ", smtlgtarr[lgtlength])

Python Numpy Array Minimum and Maximum Value output

Numpy Array Items =  [ 99 120  50   9 428  16 190]
The Smallest Number in smtlgtarr Numpy Array =  9
The Largest Number in smtlgtarr Numpy Array =  428

In this Python example, we assigned the first value to the smallest and largest variables. The if condition (if(largest < smtlgtarr[I])) checks whether the current numpy array item is less than the largest. If True, assign that value to the largest variable (largest = smtlgtarr[I]) and the index value to the larposition (larposition = i) variable. The next if condition (if(smallest > smtlgtarr[I])) check item is less than the smallest. If True, (smallest = smtlgtarr[I]) change the smallest value and assign that (smtposition = i) index position.

# Smallest Array Item

import numpy as np

smtlgtarr = np.array([99, 120, 625, 150, 9, 428, 716, 190])
print("Numpy Array Items = ", smtlgtarr)

smallest = smtlgtarr[0]
largest = smtlgtarr[0]

for i in range(1, len(smtlgtarr) - 1) :
    if(largest < smtlgtarr[i]) :
        largest = smtlgtarr[i]
        larposition = i
    if(smallest > smtlgtarr[i]) :
        smallest = smtlgtarr[i]
        smtposition = i
        
print("The Smallest Number in this Numpy Array   = ", smallest)
print("The Index Position of the Smallest Number = ", smtposition)
print("The Largest Number in this Numpy Array    = ", largest)
print("The Index Position of the Largest Number  = ", larposition)

Python Numpy Array Largest and Smallest Value output

Numpy Array Items =  [ 99 120 625 150   9 428 716 190]
The Smallest Number in this Numpy Array   =  9
The Index Position of the Smallest Number =  4
The Largest Number in this Numpy Array    =  716
The Index Position of the Largest Number  =  6