Python program to find the largest element of a list

 

Python program to find the largest element of a list.

Problem Definition

In this program reads the array elements of the list. Then find the largest of elements of the list and finally prints the elements of the list and largest element.

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

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

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

3. Read the actual elements of the list one by one from 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.

5. Finally, print the list elements and largest of elements of a list.

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)
print ("Largest of elements of ", l1, "is: ", largest )

Detailed Program Explanation

First, 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. The user must enter a numeric value, otherwise, it generates an error.

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

Finally, calculate the largest of elements of a list using the inbuilt max function of the list and print the list and largest elements of the list.

Program Source code without using inbuilt list function

largest = 0
l1 = list()

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
print ("Largest of elements of ", l1, "is: ", largest )

Detailed Program Explanation

First, create a variable called largest=0 to store the largest element 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. The user must enter a numeric value, otherwise, it generates an error.

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 element. If the current element is larger than the largest, then set the largest to the current element.

Finally, print the elements of the list and the largest of elements 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: 20
Enter the element: 60
Enter the element: 40
Largest of elements of  [10, 20, 60, 40] is:  60

If you like the tutorial please share it with your friends and share your opinion in the comment box.

Leave a Comment

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