In this program, we will take a user input as a list and find the minimum and maximum elements from it with the help of the below-given steps.
Table of Contents
Minimum and maximum element in list:
Method 1 : Sorting and indexing
Steps to solve the program
- Take a list as input.
- Sort the list using Sort().
- Print the maximum value i.e. element with -1 index.
- Print the minimum value i.e. element with 0 index.
#Input list
list1 = [23,56,12,89]
#Sorting list
list1.sort()
#Printing output
print("Smallest number: ", list1[0])
print("Largest number: ", list1[-1])
Output :
Smallest number: 12
Largest number: 89
Minimum and maximum element in list:
Method 2 : Using in-built function
Steps to solve the program
- Take a list as input.
- Print the maximum value using max().
- Print the minimum value using min().
#Input list
list1 = [23,56,12,89]
#Printing output
print("Smallest number: ", min(list1))
print("Largest number: ", max(list1))
Output :
Smallest number: 12
Largest number: 89
Related Articles
Python program to differentiate even and odd elements from the given list.
Python program to remove all duplicate elements from the list.
Python program to print a combination of 2 elements from the list whose sum is 10.
Python program to print squares of all even numbers in a list.
Python program to get common elements from two lists.