Python Program to Find the Largest Number in a List
This article was created to cover some programs in Python that find and print the largest number (or element) in a given list. Here is a list of programs covered:
- Find the largest number in the list without using the max() method.
- Find the largest number in a list using the max() method.
Find the largest number in the list without max()
The question is: write a Python program to find the largest element in a list using the for loop. The program given below is the answer to this question:
nums = []
print("Enter 10 Elements (Numbers) for List: ")
for i in range(10):
nums.append(int(input()))
large = nums[0]
for i in range(10):
if large<nums[i]:
large = nums[i]
print("\nLargest Number is: ")
print(large)
Here is its sample run:
Now supply the input, say 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 as ten numbers for the list. Now press the ENTER key to find and print the largest number from the given list of numbers:
Find the largest number in a list using max()
The question is: write a program in Python that finds and prints the largest number in a list using the max() method. The following program is the answer to this question:
nums = []
print(end="Enter the Size of List: ")
listSize = int(input())
print(end="Enter " +str(listSize)+ " Elements for List: ")
for i in range(listSize):
nums.append(int(input()))
large = max(nums)
print("\nLargest Number = " + str(large))
Here is its sample run with user input, with 5 as the size of the list and 1, 2, 3, 4, and 0 as the five elements of the list:
Python Online Test
« Previous Program
Next Program »