Python program to find the largest smallest average of a list

Python program to find the largest smallest average of a list

Problem Definition

This program reads the array elements of the list. Then find the largest, smallest, and average of elements of the list and finally print the elements of the list, largest element, smallest element, and an average of the list.

Step by Step solution to Python program to find the largest smallest average of a list

1. Create an empty list to store the elements of the list, the largest variable with 0, smallest to 999, and average to 0.

2. Read the number of elements present in the list.

3. Read the actual elements of the list one by one using a standard keyboard.

4. Loop over the elements of the list, add check whether the element is larger than the current maximum number. if the element is larger than max, set the max with the current element of the list.

See also  Python program to find the largest of three numbers

5. Finally, print the list elements, largest, smallest element, and average of the list.

Program Source code using the inbuilt list function

l1 = list()

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

largest  = max(l1)
smallest = min(l1)
average = sum(l1) / len(l1)
print ("Largest of elements of ", l1, "is: ", largest )
print ("Smallest of elements of ", l1, "is: ", Smallest )
print ("Average of list", l1, "is: ", average )

Detailed Program Explanation

First, create an empty list say l1 using the list function to store the elements of the list.

Read the number of elements in the list using the input function.

Loop over the number of elements, read the actual elements of the list one by one using the input function.

Finally, calculate the largest, smallest, average of elements of a list using the inbuilt max, min, sum, and len function of the list and print the list and largest elements of the list.

See also  Python program to find the second largest element, the cumulative sum of elements

Program Source code without using the inbuilt list function

l1 = list()
largest = 0
smallest = 999
s = 0

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

for ele in l1:
    if (ele > largest ):
        largest = ele
    if (ele < smallest):
       smallest= ele
    s = s + ele
average = s / len(l1)
print ("Largest of elements of ", l1, "is: ", largest )
print ("Smallest of elements of ", l1, "is: ", Smallest )
print ("Average of list", l1, "is: ", average )

Detailed Program Explanation

First, create a variable called largest=0, smallest=999, s=0, and average=0 and create an empty list using the list function to store the elements of the list.

Read the number of elements in the list using the input function.

Loop over the number of elements, read the actual elements of the list one by one using the input function.

Loop over the elements of a list using for loop, compare the current against the largest and smallest element. If the current element is larger than the largest, then set largest to the current element. If the current element is smaller than the smallest, then set the smallest to the current element.

See also  Classes and functions in Python

Finally, print the elements of the list, the largest, the smallest element, and the average of the list.

The output of the Python program to find the largest element of a list

Enter the number of elements:4
Enter the element: 10
Enter the element: 30
Enter the element: 60
Enter the element: 40
Largest of elements of [10, 30, 60, 40] is 60
Smallest of elements of [10, 30, 60, 40] is 10
Average of elements of [10, 30, 60, 40] is 35

If you like the tutorial please share it with your friends and share your opinion in the comment box. If you want to get the regular update regarding VTU CBCS notes, question papers, Interview preparation like ourĀ Facebook page.

Leave a Comment

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