In this tutorial, we will see various Python programs to find the smallest number in a List. For example, if the list is [15, 20, 10, 16] then the program should display number 10 as the output (the smallest number in the given list).
Example 1: Finding smallest number in a list using sort() method
In the following program, we are finding the smallest number in the given list using sort() function. The sort() function sorts the given list in ascending order. We are then displaying the first element of the list, which is the smallest number of the sorted list.
# Python program to find smallest number in a list # Given list of numbers lis = [89, 100, 35, 16, 99] # sorting the given list "lis" # sort() function sorts the list in ascending order lis.sort() # Displaying the first element of the list # which is the smallest number in the sorted list print("Smallest number in the list is:", lis[0])
Output:
Example 2: Finding smallest number in a list using min() method
In the following program we are finding the smallest number of the list using min() method. The min() method returns the smallest element of the list.
# Python program to find smallest number in the # given list using min() method # Given list of numbers lis = [9, 100, 3, 16, 60] # min() method returns the smallest element of the list print("Smallest number of the list is:", min(lis))
Output:
Example 3: Finding smallest number in a list, where list is provided by user
In the following program we are finding the smallest number in the user provided list, user is asked to enter the number of elements to put in a list. User then enters the elements of the list one by one, each input element is appended to the list using append() method. At the end of the program, the smallest number of the list is displayed using min() method.
# creating empty list lis = [] # user enters the number of elements to put in list count = int(input('How many numbers? ')) # iterating till count to append all input elements in list for n in range(count): number = int(input('Enter number: ')) lis.append(number) # displaying smallest element print("Smallest element of the list is :", min(lis))
Output:
Leave a Reply