Here,We are going to write python program to find the largest and smallest number in a list/tuple. There are many approaches to solve the above python query.
We can Sort the list using sort method and Sort element in ascending order and print the first element and last in the list. We can also use min() and max() function to find largest and smallest element in list
Python program to find the largest/ smallest number in a list/tuple
# creating empty list
listn = []
# 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: "))
listn.append(element)
# print Smallest element
print("Smallest element in List is:", min(listn))
print("Largest element in List is:", max(listn))
Output
>>> %Run smalllargeinlist.py Enter number of elements in list: 5 Enter elements: 56 Enter elements: 44 Enter elements: 33 Enter elements: 77 Enter elements: 22 Smallest element in List1 is: 22 Largest element in List1 is: 77 Enter number of elements in list: 10 Enter elements: 55 Enter elements: 77 Enter elements: 33 Enter elements: 88 Enter elements: 99 Enter elements: 22 Enter elements: 45 Enter elements: 67 Enter elements: 34 Enter elements: 52 Smallest element in List is: 22 Largest element in List is: 99
<