Last updated on February 8th, 2022 at 11:22 am
Find the smallest/largest number in a list/tuple
# Program to Find the smallest number in a list
There may be many approaches to find the smallest number in a list. Here I provide some of the approaches.
Approach 1: You can Sort the list using sort method. Sort in ascending order and print the first element in the list.
# Python program to find smallest number in a list using sort method
# list of numbers
list1 = [7, 10, 12, 3, 100, 95]
# sorting the list
list1.sort()
# printing the first element
print("Smallest element is:", list1[:1])Output:
('Smallest element is:', [3])
Approach 2 : Using min() method
# Python program to find smallest number in a list
# list of numbers
list1 = [7, 10, 12, 3, 100, 95]
# printing the minimum element
print("Smallest element is:", min(list1))Output:
('Smallest element is:', 3)
>>>
Approach 3 :
#Creating List by user input and Find min list element.
# creating empty list
list1 = []
# Input number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
element= int(input("Enter elements: "))
list1.append(element)
# print Smallest element
print("Smallest element in List1 is:", min(list1))
Output:
Enter number of elements in list: 5
Enter elements: 36
Enter elements: 53
Enter elements: 49
Enter elements: 22
Enter elements: 29
('Smallest element in List1 is:', 22)
>>>You may also Check :
Input a list of numbers and swap elements at the even location with the elements at the odd location.
Input a list/tuple of elements, search for a given element in the list/tuple.
Input a list of numbers and find the smallest and largest number from the list.
Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have scored marks above 75.