Write a program in python to find the maximum and minimum elements in the list entered by the user

Python program to find the maximum and minimum element in a list

Python to Find Minimum and Maximum 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 theminandmaxfunctions 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
Recommended:-Python Program to Find Smallest/Minimum of n Numbers

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

Python | Maximum and minimum element’s position in a list

Given a list of N integers, find the maximum and minimum element’s position in the list.

Examples:

Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 3

Python: Find the maximum and minimum numbers from a sequence of numbers

Last update on September 01 2020 10:27:16 [UTC/GMT +8 hours]

How to find max and min from a list

Finding the maximum and minimum values in a list is pretty easy because of the built-in max and min functions provided by Python. However, you may be wondering what the logic behind these functions is. Let me explain it with a couple of code snippets.

# Pass a list to this function to check for maximum number def max_check[x]: max_val = x[0] for check in x: if check > max_val: max_val = check return max_val
Function to find maximum number of a list.
# Pass a list to this function to check for minimum number def min_check[x]: min_val = x[0] for check in x: if check < min_val: min_val = check return min_val
Function to find minimum number of a list.

To validate our above defined functions, let’s pass a list to them.

# Pass a list to this function to check for maximum number def max_check[x]: max_val = x[0] for check in x: if check > max_val: max_val = check return max_val # Pass a list to this function to check for minimum number def min_check[x]: min_val = x[0] for check in x: if check < min_val: min_val = check return min_val #List my_list = [2, 6, 8, 14, 3, 77, 63] #Printing Values print["Maximum of the list", max_check[my_list]] print["Minimum of the list", min_check[my_list]]
Run
Validate the max and min functions.

Note: The functions will generate an error for empty lists.

Python program to find the largest and smallest number in a list

Hello everybody, this is a Python program which finds out the smallest and largest number in the list.

Smallest and Largest number in list — programminginpython.com

Here we use 2 predefined functions min[] and max[] which check for the smallest and largest number in a list respectively.

Video liên quan

Bài mới nhất

Chủ Đề