Python program to print Largest and Smallest numbers in a list using the max and min function
In this article, we will try to print the largest and the smallest numbers in a given list using the inbuilt functions. We will make a function let's name it largestandsmallest, and ask the user for input for the list.
def largestandsmallest():
List = [] #create empty list
Total_no = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Total_no + 1):
value = int(input("Enter the Value of %d Element : " %i))
List.append(value)
print("The Largest Element in List is : ", max(List))
print("The Smallest Number in List is : ", min(List))
largestandsmallest()
After inputting the list we will append the list items one by one using the append function. And at the last using the default functions max() and min() we will print the largest and smallest numbers respectively.
Here's the final result, there are many other ways to do find the smallest and largest without using the functions we will see in the next article.