Python program to print maximum and minimum of a list

 

Python program to print maximum and minimum element of a list

Problem Definition

Python program to print maximum and minimum of a list, first read numbers from the user until the user enters ‘done’ and store the numbers into a list. Once the user enters ‘done’ print out the maximum and minimum element of a list.

Solution

Step by Step solution to Python program to print maximum and minimum elements of a list without using the list inbuilt functions

Initially set the variable max to 0, variable min to 999, and create an empty list l1.

Read the number of elements in the list and the elements of the list one by one from the user using the for and input statement.

If the input entered is is other than ‘done’, then convert the input to an integer value using int function and append the input value to list l1.

If the input is ‘done’ break the loop. Finally, print the maximum and minimum elements of the list of numbers entered by the user.

Read the elements of list l1 and compare against the variable max and min. If the value of the input is greater than max, then set max to the input value. If the input value is smaller than the min then set the input value to variable min.

max = 0
min = 999
l1 = list()

while (True):
    ele = input ("Enter the element: ")
    if ele == 'done': 
        break
    ele = int(ele)
    l1.append(ele)

for ele in l1:
    if (ele > max):
        max  = ele
    if (ele < min):
        min = ele
print ("Maximum elements of ", l1, "is: ", max)
print ("Minimum elements of ", l1, "is: ", min)

Output

Enter the element: 10

Enter the element: 7

Enter the element: 12

Enter the element: done

Maximum elements of [10, 7, 12] is: 12

Minimum elements of [10, 7, 12] is: 7

Step by Step solution to Python program to print maximum and minimum element of a list using the list inbuilt functions

Initially create an empty list l1.

Read the number of elements in the list and the elements of the list one by one from the user using the for and input statement.

If the input entered is is other than ‘done’, then convert the input to an integer value using the int function. The input value is appended to list l1. If the input is ‘done’ break the loop.

Finally, print the maximum and minimum elements of the list of numbers entered by the user using the max and min functions of the list.

l1 = list()
n = int(input ("Enter the number of elements:"))
for i in range(n):
    inp = input("Enter a number:")
    if inp == 'done':
        break
    inp = float(inp)
    l1.append(inp)

print ("The largest element is:", max(l1))
print ("The smallest element is:", min(l1))

Output

Enter the number of elements: 10

Enter a number: 7

Enter a number:12

Enter a number: done

The largest element is: 12.0

The smallest element is: 7.0

Another solution using the number of elements in the list. Here the terminating condition to read the elements of a list is a number of elements. In the earlier case, the keyword ‘done’ from the user is the terminating condition.

l1 = list()

n = int(input ("Enter the number of elements:"))
for i in range(n):
    ele = int (input ("Enter the element: "))
    l1.append(ele)

average = sum(l1) / len(l1)
print ("Maximum elements of ", l1, "is: ", max(l1) )
print ("Minimum elements of ", l1, "is: ", min(l1) )
print ("Average of list", l1, "is: ", average )

Output

Enter the number of elements: 3

Enter the element: 10

Enter the element: 7

Enter the element: 13

Maximum elements of [10, 7, 13] is: 13

Minimum elements of [10, 7, 13] is: 7

Average of list [10, 7, 13] is: 10.0

Summary:

If you like the program please share it with your friends and share your opinion in the comment box. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds