In this Python program, we will learn how to find the largest and smallest number in a given list. In this program, we will use python built-in functions such as max()
, min()
, sort()
, or without using any built-in function to find the largest and smallest number in a given list.
Here is the source code of the program to find the largest and smallest number in a given list.
# Python Program to find the Largest and Smallest Number in a List using max() and min() function
NumList = []
# Take the input from user
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList: ",NumList)
print("\nThe Smallest Element in this List is: ", min(NumList))
print("The Largest Element in this List is: ", max(NumList))
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element: 62
Enter the Value of 2 Element: 641
Enter the Value of 3 Element: 8451
Enter the Value of 4 Element: 1233
Enter the Value of 5 Element: 4613
Enter the Value of 6 Element: 852
List: [62, 641, 8451, 1233, 4613, 852]
The Smallest Element in this List is: 62
The Largest Element in this List is: 8451
# Python Program to find the Largest and Smallest Number in a List using sort() function
NumList = []
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element : " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList: ",NumList)
NumList.sort()
print("\nThe Smallest Element in this List is : ", NumList[0])
print("The Largest Element in this List is : ", NumList[Number - 1])
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element : 123
Enter the Value of 2 Element : 523
Enter the Value of 3 Element : 4562
Enter the Value of 4 Element : 665
Enter the Value of 5 Element : 265
Enter the Value of 6 Element : 452
List: [123, 523, 4562, 665, 265, 452]
The Smallest Element in this List is : 123
The Largest Element in this List is : 4562
# Python Program to find Largest and Smallest Number in a List Without Using any Built-in Function
NumList = []
# Take the input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList: ",NumList)
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)
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element: 456
Enter the Value of 2 Element: 123
Enter the Value of 3 Element: 523
Enter the Value of 4 Element: 458
Enter the Value of 5 Element: 689
Enter the Value of 6 Element: 475
List: [456, 123, 523, 458, 689, 475]
The Smallest Element in this List is: 123
The Index position of Smallest Element in this List is: 1
The Largest Element in this List is: 689
The Index position of Largest Element in this List is: 4
Comments