Python program to find the largest smallest average of a list

Python program to find the largest, smallest, average of a list.

Problem Definition

In this program reads the array elements of the list. Then find the largest, smallest and average of elements of the list and finally prints 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 list, 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 list one by one using 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.

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

READ  Identifiers in Python Programs

Program Source code using 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 lenfunction of the list and print the list and largest elements of the list.

Program Source code without using 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 store the elements of the list.

READ  Explain the init and str method with an example python program

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

Loop over the number of elements, read the actual elements of 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 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.

Finally, print the elements of the list, largest, 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 with your friends and share your opinion in the comment box.

READ  Structured Query Language with a python program