Python Program to Find Maximum and Minimum Number in List

Python program to find maximum and minimum number in a list or array; Through this tutorial, i am going to show you how to find largest or maximum and smallest or minimum number in python using for loop, min(), max() function.

Python Program to Find Maximum and Minimum Number in List

  • Python program to find maximum and minimum number in an array
  • Find max and min in a list python using for loop
  • Python program to find maximum and minimum number in list using sort method
  • Python Program to find the position of min and max elements of a list using min() and max() function

Python program to find maximum and minimum number in an array

  • Get input list of element from user.
  • Next, iterate the for loop and add the number in the list.
  • Use the min and max functions to find the largest and smallest numbers from the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List 
NumList = []
Number = int(input("Please enter element length in list "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
print("The Smallest Element in this List is : ", min(NumList))
print("The Largest Element in this List is : ", max(NumList))

After executing the program, the output will be:

How many element in list, Please enter num :-  5
Please enter the Value of 1 Element :  500
Please enter the Value of 2 Element :  653
Please enter the Value of 3 Element :  895
Please enter the Value of 4 Element :  565
Please enter the Value of 5 Element :  568

The Smallest Element in this List is :  500
The Largest Element in this List is :  895

Find max and min in a list python using for loop

  • Get input list of element in list from user.
  • Next, iterate the for loop and add the number in the list.
  • Iterate for loop with list and use if statement to find the min and max number and their position in the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List 
# Python Program to find Largest and Smallest Number in a List 
NumList = []
Number = int(input("How many element in list, Please enter num :- "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
smallest = largest = NumList[0]
for j in range(1, Number):
    if(smallest > NumList[j]):
        smallest = NumList[j]
        min_position = j
    if(largest < NumList[j]):
        largest = NumList[j]
        max_position = j
print("The Smallest Element in this List is : ", smallest)
print("The Index position of Smallest Element in this List is : ", min_position)
print("The Largest Element in this List is : ", largest)
print("The Index position of Largest Element in this List is : ", max_position)

After executing the program, the output will be:

How many element in list, Please enter num :-  5

Please enter the Value of 1 Element :  70
Please enter the Value of 2 Element :  90
Please enter the Value of 3 Element :  60
Please enter the Value of 4 Element :  80
Please enter the Value of 5 Element :  45
The Smallest Element in this List is :  45
The Index position of Smallest Element in this List is :  4

The Largest Element in this List is :  90
The Index position of Largest Element in this List is :  1

Python program to find maximum and minimum number in list using sort method

  • Get input list of element from user.
  • Next, iterate the for loop and add the number in the list.
  • Use python sort method to find the smallest and largest number from the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List 
NumList = []
Number = int(input("How many element in list, please enter num :- "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
NumList.sort()
print("The Smallest Element in this List is : ", NumList[0])
print("The Largest Element in this List is : ", NumList[Number - 1])

After executing the program, the output will be:

How many element in list, please enter num :-  5

Please enter the Value of 1 Element :  98
Please enter the Value of 2 Element :  45
Please enter the Value of 3 Element :  63
Please enter the Value of 4 Element :  78
Please enter the Value of 5 Element :  25

The Smallest Element in this List is :  25
The Largest Element in this List is :  98

Python Program to find the position of min and max elements of a list using min() and max() function

  • Get input list of element from user.
  • Next, iterate the for loop and add the number in the list.
  • Use min and max function with index function to find the position of an element in the list.
  • Print the results.
# Python Program to find Largest and Smallest Number in a List 
NumList = []
Number = int(input("How many element in list, Please enter num :- "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
# min element's position/index
min = NumList.index(min(NumList))
# max element's position/index
max = NumList.index(max(NumList))
# printing the position/index of min and max elements
print("position of minimum element: ", min)
print("position of maximum element: ", max)

After executing the program, the output will be:

How many element in list, Please enter num :-  5

Please enter the Value of 1 Element :  5
Please enter the Value of 2 Element :  8
Please enter the Value of 3 Element :  9
Please enter the Value of 4 Element :  4
Please enter the Value of 5 Element :  2

position of minimum element:  4
position of maximum element:  2

Recommended Python Tutorials

Be First to Comment

Leave a Reply

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