In this article, we will learn to find the largest number in a list using the python program.
We find the largest number in a list using 3 different methods:
- Using max() function
- By comparing each element
- By using sort() function
Using min() function
max() is the in-built function of the python, which returns the largest value of the list.
- First, we declared the list.
- Then, we use the max() function to find the largest number in a list.
- Then we print the large value.
By comparing each element
In this method, we will iterate the list and compare each element to find the largest number.
- First, we declared the list and a large variable.
- And, we assume the first element as the largest number and assign them to the large variable.
- Then we run the for loop and check each element of the loop is greater than large or not.
- If any element is larger than the large variable then we update the value of the large variable.
- Then, lastly, we prin the large variable as the largest number.
Using sort() function
sort() function used to arrange a list in ascending order. so, using the sort() function we will easily find the largest number value from the list.
- First, we declare the list.
- And then we use the sort() function to arrange the list in ascending order.
- And then we know that in ascending order the last element is always larger.
- So, we print the last element.