Saturday, November 16, 2024
Google search engine
HomeLanguagesPython to Find Minimum and Maximum Elements of List

Python to Find Minimum and Maximum Elements of List

Python program to find maximum and minimum number in a list; In this python article, we would love to show you how to find maximum and minimum number in a list in python.

Python to Find Minimum/Min and Maximum/Max Elements of List

  • Python Program to find the Largest and Smallest Number in a List using min() and max() method.
  • Python Program to find the Minimum and Maximum Number in a List with their position using for loop and if statement.
  • Python Program to find the Largest and Smallest Number in a List using sort() method.
  • Python Program to find the position of min and max elements of a list using min() and max() function.

1: Python Program to find the Largest and Smallest Number in a List using min() and max() method

  • Allow user to enter the length of the list.
  • Next, iterate the for loop and add the number in the list.
  • Use the min and max functions to find the smallest and largest 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

2: Python Program to find the Minimum and Maximum Number in a List with their position using for loop and if statement

  • Allow user to enter the length of the list.
  • 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

3: Python Program to find the Largest and Smallest Number in a List using sort() method

  • Allow user to enter the length of the list.
  • 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

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

  • Allow user to enter the length of the list.
  • 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 Posts

RELATED ARTICLES

Most Popular

Recent Comments