Find the smallest/largest number in a list/tuple Python Program

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 :

Leave a Reply

Your email address will not be published. Required fields are marked *