Write a Python Program to find the Largest and Smallest Number in a List with a practical example.
Python Program to find the Largest and Smallest Number in a List Example 1
This python program allows users to enter the length of a List. Next, we used For Loop to add numbers to the list. Here, the min and max functions return the smallest and largest numbers or minimum and maximum values in a List.
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
print("The Smallest Element in this List is : ", min(NumList))
print("The Largest Element in this List is : ", max(NumList))
Largest and smallest list number output
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 50
Please enter the Value of 2 Element : 45
Please enter the Value of 3 Element : 33
Please enter the Value of 4 Element : 78
Please enter the Value of 5 Element : 66
The Smallest Element in this List is : 33
The Largest Element in this List is : 78
Largest and Smallest Number in a List Example 2
The sort function sorts List elements in ascending order. Next, we are using index position 0 to print the first element and the last index position to print the last element in a list.
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
NumList.sort()
print("The Smallest Element in this List is : ", NumList[0])
print("The Largest Element in this List is : ", NumList[Number - 1])

Python Program to find the Largest and Smallest Number in a List Example 3
In this program, we are not using any built-in function such as sort, max, or min function.
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
smallest = largest = NumList[0]
for j in range(1, Number):
if(smallest > NumList[j]):
smallest = NumList[j]
min_position = j
if(largest < NumList[j]):
largest = NumList[j]
max_position = j
print("The Smallest Element in this List is : ", smallest)
print("The Index position of Smallest Element in this List is : ", min_position)
print("The Largest Element in this List is : ", largest)
print("The Index position of Largest Element in this List is : ", max_position)
The largest and smallest list number output
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 40
Please enter the Value of 2 Element : 60
Please enter the Value of 3 Element : 20
Please enter the Value of 4 Element : 11
Please enter the Value of 5 Element : 50
The Smallest Element in this List is : 11
The Index position of Smallest Element in this List is : 3
The Largest Element in this List is : 60
The Index position of Largest Element in this List is : 1
From the above Python Program to find the Largest and Smallest Number in a List output, the User inserted values are
NumList[5] = {40, 60, 20, 11, 50}
smallest = largest = NumList[0] = 40
First Iteration – for 1 in range(1, 5) – Condition is true
So, it starts executing the If statement inside the loop until the condition fails.
If (smallest > NumList[j]) inside the For Loop is False because (40 > 60)
smallest = 40
position = 1
If (largest < NumList[j]) inside the for loop is True because (40 < 60)
largest = 60
position = 1
Second Iteration: for 2 in range(1, 5) – Condition is true
If (40 > 20) – Condition True
smallest = 20
Position = 2
If (60 < 20) – Condition False
largest = 60 ==> unchanged
Position = 1 ==> unchanged
Third Iteration: for 3 in range(1, 5) – Condition is true
If (20 > 11) – Condition True
smallest = 11
Position = 3
If (60 < 11) – Condition False
largest = 60
Position = 1
Fourth Iteration: for 4 in range(1, 5) – Condition is true
If (11 > 50) – Condition False
smallest = 11
Position = 3
If (60 < 11) – Condition False
largest = 60
Position = 1
Fifth Iteration: for 5 in range(1, 5) – Condition is False
So, it exits from the loop.