;

Python Program to Find the Largest Number in a Given List


Tutorialsrack 30/04/2020 Python

In this python program, we will find the largest number in a given list. There are various ways to find the largest number in a given list. In this program, we use loops and python built-in functions such as max(), sort(), reverse()

Here is the source code of the program to find the largest number in a given list.

Program 1: Python Program to find Largest Number in a Given List Using a built-in max() function

Python Program to find Largest Number in a Given List Using a built-in max() function
# Python Program to find Largest Number in a List Using a built-in max() function

a = [104, 785, 452, 152, 852, 195]

# Print the List
print("List: ",a)
print("The Largest Element in this List is: ", max(a))
Output

List:  [104, 785, 452, 152, 852, 195]

The Largest Element in this List is:  852

Program 2: Python Program to find Largest Number in a Given List Using For Loop and max() Function

In this program, we will take the input from the user and find the largest number in a given list using the max() function.

Python Program to Find Largest Number in a Given List Using For Loop and max() Function

# Python Program to find Largest Number in a Given List Using For Loop and max() Function

NumList = []

# Take the Input from the Number
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)
    
# Print the List Entered By the User
print("List: ",NumList)

print("The Largest Element in this List is: ", max(NumList))
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 45

Enter the Value of 2 Element: 15

Enter the Value of 3 Element: 65

Enter the Value of 4 Element: 62

Enter the Value of 5 Element: 48

Enter the Value of 6 Element: 35

List:  [45, 15, 65, 62, 48, 35]

The Largest Element in this List is:  65

Program 3: Python Program to find Largest Number in a Given List Using the sort() function

Python Program to find Largest Number in a Given List Using the sort() function
# Python Program to find Largest Number in a Given List using the sort() function

lst=[]

# Take the Input from th User
number=int(input("Enter the Total Number of List Elements: "))
for i in range(1,number+1):
    b=int(input("Enter the Value of %d Element: "%i))
    lst.append(b)

# Print the List Entered By the User
print("List: ", lst)
    
lst.sort()
print("The Largest Element in this List is: ",lst[number-1])
Output

The Largest Element in this List is:  6

Enter the Total Number of List Elements: 5

Enter the Value of 1 Element: 45

Enter the Value of 2 Element: 52

Enter the Value of 3 Element: 36

Enter the Value of 4 Element: 85

Enter the Value of 5 Element: 456

List:  [45, 52, 36, 85, 456]

The Largest Element in this List is:  456

Program 4: Python Program to find Largest Number in a Given List Using sort() and reverse() function

In this program, we take the input from the user and find the largest number from the given list by using python built-in sort() and reverse() functions where sort() function sort the list in the ascending order and reverse function reverse the sorted list.

Python Program to find Largest Number in a Given List Using sort() and reverse() function
# Python Program to find Largest Number in a Given List using sort() and reverse() function

NumList = []
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)

# print the List Entered By the User
print("\nList: ", NumList)

# Sort the List in Ascending Order
NumList.sort()

# Reverse the List
NumList.reverse()

print("The Largest Element in this List is : ", NumList[0])
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 23

Enter the Value of 2 Element: 52

Enter the Value of 3 Element: 48

Enter the Value of 4 Element: 321

Enter the Value of 5 Element: 526

Enter the Value of 6 Element: 462

 

List:  [23, 52, 48, 321, 526, 462]

The Largest Element in this List is :  526

Program 5: Python Program to find Largest Number in a Given List Without using any Function

Python Program to find Largest Number in a Given List Without using any Function
# Python Program to find Largest Number with its Index Position in a Given List Without using any Function

NumList = []

# Take the Input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of %d Element: " %i))
    NumList.append(value)

# Print the List Entered by the User.
print("\nList: ", NumList)

largest = NumList[0]    
for j in range(1, Number):
    if(largest < NumList[j]):
        largest = NumList[j]
        position = j

print("The Largest Element in this List is : ", largest)
print("The Index position of the Largest Element is : ", position)
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: 521

Enter the Value of 2 Element: 652

Enter the Value of 3 Element: 325

Enter the Value of 4 Element: 452

Enter the Value of 5 Element: 854

Enter the Value of 6 Element: 653

 

List:  [521, 652, 325, 452, 854, 653]

The Largest Element in this List is :  854

The Index position of the Largest Element is :  4


Related Posts



Comments

Recent Posts
Tags