Python Program to Find Largest Number in an Array

Write a Python Program to Find Largest Number in an Array. The numpy module has a max function that returns the largest or maximum value in an array. We use this numpy max function to return the largest item in a number array and string array. 

# Largest Array Item
import numpy as np
lgtarr = np.array([50, 65, 14, 27, 99, 10, 4, 195, 18, 100])
print("Numeric Largest Numpy Array Items = ", lgtarr)
print("The Largest Number in this Numpy Array = ", max(lgtarr))

strlgtarr = np.array(['UK','India', 'Japan', 'USA'])
print("String Largest Numpy Array Items = ", strlgtarr)
print("The Largest Number in this Numpy Array = ", max(strlgtarr))
Python Program to Find Largest Number in an Array 1

Python Program to Find Largest Number in an Array.

We used the numpy sort function to sort the array in ascending order and printing the number at the last index position, which is the largest.

# Largest Array Item
import numpy as np
lgtarr = np.array([50, 15, 22, 270, 199, 195, 100])
print("Numeric Largest Numpy Array Items = ", lgtarr)

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

print("The Largest Number in this Numpy Array = ", lgtarr[lgtlength])

Largest Python Numpy Array item output

Numeric Largest Numpy Array Items =  [ 50  15  22 270 199 195 100]
The Largest Number in this Numpy Array =  270

In this Python example, we assigned the first value as the largest, and the for loop range starts at one and traverses uptown lgtarr length minus. The if condition (if(largest < lgtarr[I])) examines whether the current numpy array element is less than the largest. If True, assign that value to the largest variable (largest = lgtarr[i]) and the index value to the larposition variable (larposition = i).

# Largest Array Item
import numpy as np
lgtarr = np.array([270, 199, 220, 195, 1200, 1750, 100])
print("Numeric Largest Numpy Array Items = ", lgtarr)

largest = lgtarr[0]

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

Largest Python Numpy Array number output

Numeric Largest Numpy Array Items =  [ 270  199  220  195 1200 1750  100]
The Largest Number in this Numpy Array   =  1750
The Index Position of the Largest Number =  5