- Python Program to Find Largest Number in a List
- Example 1: Finding largest number in a list using sort() method
- Example 2: Finding largest number in a list using max() method
- Example 3: Finding largest number in a list, where list is provided by user
- Related Python Examples
- Top Related Articles:
- About the Author
- Python Program to Find Largest Number in a List
- Examples
- 1. Find the largest number in given list using sort()
- 2. Find largest number in list using For loop
- Summary
- Find Largest and Smallest Number in Python Without List
- Largest and Smallest Number in Python Without List For Loop
- Largest and Smallest Number in Python Without List min and max
- Largest and Smallest Number in Python Without List sorted()
- Conclusion
Python Program to Find Largest Number in a List
In this tutorial, we will see various Python programs to find the largest number in a List. For example, if the list is [5, 10, 30, 6] then the output of the program should be 30 (the largest number in the given list).
Example 1: Finding largest number in a list using sort() method
In the following program we are using the sort() function to find the largest number in the given list. A list of numbers is given in the program and we are sorting the given list using sort() function, which sorts the list in ascending order. We are then displaying the last element of the list, which is the largest number in the sorted list.
# Python program to find largest number in a list # A list of numbers is given lis = [1, 10, 40, 36, 16] # sorting the given list "lis" # sort() function sorts the list in ascending order lis.sort() # Displaying the last element of the list # which is the largest number in the sorted list print("Largest number in the list is:", lis[-1])
Output:
Example 2: Finding largest number in a list using max() method
In the following program we are finding the largest number of the list using max() method. The max() method returns the largest element of the list.
# Python program to find largest number in a list # using max() function # A list of numbers lis = [19, 10, 45, 26, 6] # max() method returns the largest element of the list print("Largest number of the list is:", max(lis))
Output:
Example 3: Finding largest number in a list, where list is provided by user
In the following example we are finding the largest number in the user provided list. In the program, user is asked to enter the number of elements to put in a list, then user enters the elements of the list which are appended to the list using append() method. At the end the largest element is displayed using max() method.
# creating empty list lis = [] # user enters the number of elements to put in list count = int(input('How many numbers? ')) # iterating till count to append all input elements in list for n in range(count): number = int(input('Enter number: ')) lis.append(number) # displaying largest element print("Largest element of the list is :", max(lis))
Output:
Related Python Examples
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Python Program to Find Largest Number in a List
You can find largest number of a list in Python using sort() function or more basic for loop.
Using sort() function is kind of concise, but using For Loop is efficient. We will go through these two approaches with examples.
Examples
1. Find the largest number in given list using sort()
We know that sort() builtin function sorts a list in ascending or descending order. After you sort a list, you have your largest number at the end of the list if you have sorted in ascending order or at the start of the list if you have sorted in descending order.
In the following example, we will sort the given list in ascending order. Of course, last number of the sorted list is our required largest number.
Python Program
# list a=[18, 52, 23, 41, 32] # sort the list, default is in ascending order a.sort() # largest number is the last item in the sorted list ln = a[-1] # print the largest number print("Largest element is: ",ln)
a[-1] fetches the last element in the list.
2. Find largest number in list using For loop
Though finding the largest number using sort() function is easy, using Python For Loop does it relatively faster with less number of operations. Also, the contents of the list are unchanged.
Python Program
a = [18, 52, 23, 41, 32] #variable to store largest number ln = a[0] if a else None #find largest number for i in a: if i>ln: ln=i print("Largest element is: ",ln)
In this example, we have assumed a list and initialized largest number ln variable to the first element of the list. If there are no elements in the list, ln is initialized with None.
Iterate the loop for each element in the list. During each iteration, we check if the largest number is less than this element. If that is the case, we update our largest number with the element.
When you complete traversing the list, you will end up with the largest number of the list in your variable.
Summary
In this tutorial of Python Examples, we learned some of the ways to find largest number in Python list with the help of well detailed Python example programs.
Find Largest and Smallest Number in Python Without List
Finding the largest and smallest numbers in a sequence of numbers can be a useful task in many programming projects. However, what if you don’t have a list or array, but instead have a sequence of numbers that you want to find the largest and smallest of?
In this Python tutorial, we will explore a few different ways to find the largest and smallest numbers in a sequence of numbers without using a list in Python.
- Largest and Smallest Number in Python Without List For Loop
- Largest and Smallest Number in Python Without List min and max
- Largest and Smallest Number in Python Without List sorted()
Largest and Smallest Number in Python Without List For Loop
One way to find the largest and smallest numbers in a sequence is to use a for loop to iterate through the numbers, keeping track of the current largest and smallest numbers as you go.
Let’s take an example by following the below steps:
Define variable numbers that contain the sequence of numbers you want to find the largest and smallest of.
Create two variables largest and smallest and initialize them with the first number in the numbers sequence.
largest = numbers[0] smallest = numbers[0]
Use a for loop to iterate through the numbers sequence.
for number in numbers: if number > largest: largest = number elif number < smallest: smallest = number
In the for loop, use an if-else statement to check if the current number is greater than the current value of the largest. If it is, update the largest variable with the current number.
In the same if-else statement, check if the current number is less than the current value of the smallest. If it is, update the smallest variable with the current number.
Print the values of largest and smallest after the for loop has been completed.
print("Largest number:", largest) print("Smallest number:", smallest)
Largest and Smallest Number in Python Without List min and max
Another way to find the largest and smallest numbers in a sequence is to use the built-in max() and min() functions. Follow the below steps:
Define variable numbers that contain the sequence of numbers you want to find the largest and smallest of.
Use the built-in max() function to find the largest number in the numbers sequence, and assign the result to a variable largest.
Use the built-in min() function to find the smallest number in the numbers sequence, and assign the result to the variable smallest.
Print the values of the largest and smallest.
print("Largest number:", largest) print("Smallest number:", smallest)
Largest and Smallest Number in Python Without List sorted()
You can also use the built-in sorted() function to sort the numbers in ascending or descending order, and then use the first or last element of the sorted list to find the largest or smallest number. Follow the below steps:
Define variable numbers that contain the sequence of numbers you want to find the largest and smallest of.
Use the built-in sorted() function to sort the numbers sequence in ascending order.
numbers_sorted = sorted(numbers)
Assign the first element of the sorted list to the variable smallest.
Assign the last element of the sorted list to the variable largest.
smallest = numbers_sorted[0]
Print the values of the largest and smallest.
print("Largest number:", largest) print("Smallest number:", smallest)
You may also like to read the following Python tutorials.
Conclusion
In this Python tutorial, we have seen how to find the largest and smallest numbers in a sequence of numbers without using a list in Python. There are several other ways to find the largest and smallest numbers in a sequence of numbers in Python, and the best method will depend on your specific use case.
However, the examples mentioned in the above tutorial should provide a good starting point for finding the largest and smallest numbers in a sequence without using a list in Python.
- Largest and Smallest Number in Python Without List For Loop
- Largest and Smallest Number in Python Without List min and max
- Largest and Smallest Number in Python Without List sorted()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.