Python Program to Find Largest Number in a List

In this post, we will learn how to find the largest number in a list using Python Programming language.

For example:  If the entered list is [7, 15, 12, 24, 38], then this program will find the largest number in the list which is 38 for this case.

We will find the largest number in a list using the following approaches:

  1. Using sort() function
  2. Using max() function
  3. With User Input

So, without further ado, let’s begin this tutorial.

Python Program to Find Largest Number in a List

# Python Program to Find Largest Number in a List
a = [12, 17, 20, 24, 15]

# Sorting the list
a.sort()

# Finding largest number
largest = a[-1]

# Displaying output
print("Largest Element in the list is: ", largest)

Output

Largest Element in the list is:  24

How Does This Program Work ?

a = [12, 17, 20, 24, 15]

In this program, we defined a list having 5 elements.

a.sort()

Then, we sort the list using the sort() function. After this, the elements of the list get arranged in the ascending order.

largest = a[-1]

After sorting the elements, then clearly the last element will be the largest number. The largest number of the list gets stored in the largest named variable.

# Displaying output
print("Largest Element in the list is: ", largest)

Finally, the largest number in the list is displayed on the screen using print() function.

Python Program to Find the Largest Number in a List Using max() function

# Python Program to Find the Largest Number in a List Using max() function
a = [8, 16, 5, 24, 48, 31]

# Finding the largest element
largest = max(a)

# Displaying output
print("Largest number in a list is: ", largest)

Output

Largest number in a list is:  48

Python Program to Find Largest Number in a List With User Input

# Python Program to Find the Largest Element in a List With User Input
a = []

# Asking for input
n = int(input("Enter total no. of elements: "))
for i in range(n):
    num = int(input("Enter the element: "))
    a.append(num)
    
# Sorting the list 
a.sort()    

# Finding the largest element
largest = a[n - 1]

# Displaying the output
print("Largest element in the list is: ", largest)

Output

Enter total no. of elements: 6
Enter the element: 11
Enter the element: 72
Enter the element: 48
Enter the element: 29
Enter the element: 44
Enter the element: 53
Largest element in the list is:  72

Conclusion

I hope after going through this post, you understand how to find the largest number in a list using Python Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

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