Python program to find smallest and largest in Array

In this tutorial, you will learn how to write Python program to find largest and smallest number in an array.

To find largest and smallest in an array we find compare each elements to each other and find which one is greatest and which one is smallest.

In this program we have taken two variable largest and smallest which will hold number.

Python Program to find largest and smallest number in an array

arr = []
n = int(input("Enter the size of the array: "))

# Taking array input from user
for _ in range(n):
    element = int(input("Enter an element of the array: "))
    arr.append(element)

# Initializing largest and smallest with the first element
largest = arr[0]
smallest = arr[0]

# Finding largest and smallest
for num in arr:
    if num > largest:
        largest = num
    if num < smallest:
        smallest = num

# Displaying the result
print(f"Largest element in the array is {largest}")
print(f"Smallest element in the array is {smallest}")

Output

Enter the size of the array: 5
Enter an element of the array: 3
Enter an element of the array: 3
Enter an element of the array: 2
Enter an element of the array: 1
Enter an element of the array: 5
Largest element in the array is 5
Smallest element in the array is 1
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now