Python Program to Find Largest and Smallest Number in List
Python program to find largest and smallest number in a list is provided on this page.
Python Program To Find Largest And Smallest Number In A List
Program 1
This python program accepts numbers from user and stores them in NumbersList list.
Then we simply use max() to find largest number and min() to find smallest number.
# Python Program to find Largest and Smallest Number in a List
NumbersList = []
length = int(input("Please enter length of list : "))
for i in range(0, length):
value = int(input("Please enter value : "))
NumbersList.append(value)
print("The Smallest Number in this List is : ", min(NumbersList))
print("The Largest Number in this List is : ", max(NumbersList))
Program 2
This is another python program which finds largest and smallest numbers in list but it does without use of max() and min() functions.
# Python Program to find Largest and Smallest Number 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)
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)
Comments