- Python Program to Find the Largest and Smallest Number in a List
- Program 1: Using min() and max() functions
- Program 2: using sort() function
- Program 3: Without using any built-in function
- Python Min & Max: Find largest & smallest values (Or with loop)
- What is max() and min() in Python?
- Find Min & Mix in a list
- Min & Max of the list using a loop
- Finding Max & Min between 2 lists
- Conclusion
- 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 the Largest and Smallest Number in a List
In this Python program, we will learn how to find the largest and smallest number in a given list. In this program, we will use python built-in functions such as max() , min() , sort() , or without using any built-in function to find the largest and smallest number in a given list.
Here is the source code of the program to find the largest and smallest number in a given list.
Program 1: Using min() and max() functions
Program 1: Using min() and max() functions
# Python Program to find the Largest and Smallest Number in a List using max() and min() function NumList = [] # Take the input from 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) print("\nThe Smallest Element in this List is: ", min(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: 62
Enter the Value of 2 Element: 641
Enter the Value of 3 Element: 8451
Enter the Value of 4 Element: 1233
Enter the Value of 5 Element: 4613
Enter the Value of 6 Element: 852
List: [62, 641, 8451, 1233, 4613, 852]
The Smallest Element in this List is: 62
The Largest Element in this List is: 8451
Program 2: using sort() function
Program 2: using sort() function
# Python Program to find the Largest and Smallest Number in a List using sort() 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) NumList.sort() print("\nThe Smallest Element in this List is : ", NumList[0]) print("The Largest Element in this List is : ", NumList[Number - 1])
Output
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element : 123
Enter the Value of 2 Element : 523
Enter the Value of 3 Element : 4562
Enter the Value of 4 Element : 665
Enter the Value of 5 Element : 265
Enter the Value of 6 Element : 452
List: [123, 523, 4562, 665, 265, 452]
The Smallest Element in this List is : 123
The Largest Element in this List is : 4562
Program 3: Without using any built-in function
Program 3: Without using any built-in function
# Python Program to find Largest and Smallest Number in a List Without Using any Built-in 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) smallest = largest = NumList[0] for j in range(1, Number): if(smallest > NumList[j]): smallest = NumList[j] min_position = j if(largest < NumList[j]): largest = NumList[j] max_position = j print("The Smallest Element in this List is: ", smallest) print("The Index position of Smallest Element in this List is: ", min_position) print("The Largest Element in this List is: ", largest) print("The Index position of Largest Element in this List is: ", max_position)
Output
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element: 456
Enter the Value of 2 Element: 123
Enter the Value of 3 Element: 523
Enter the Value of 4 Element: 458
Enter the Value of 5 Element: 689
Enter the Value of 6 Element: 475
List: [456, 123, 523, 458, 689, 475]
The Smallest Element in this List is: 123
The Index position of Smallest Element in this List is: 1
The Largest Element in this List is: 689
The Index position of Largest Element in this List is: 4
Python Min & Max: Find largest & smallest values (Or with loop)
To find the biggest and tiniest item in a list, Python gives us two easy functions to do. Today we'll look at Python's built-in min() and max() functions, with the help of some examples.
What is max() and min() in Python?
The min() and max() are built-in functions of Python programming language to find the smallest and the largest elements in any iterable. These functions come in handy when working with any iterables like lists, tuples, sets, and dictionaries in Python.
The min() function takes an iterable as an argument and returns the smallest item in the iterable. Similarly, the max() function accepts an iterable as an input and returns the iterable's largest item.
The basic syntax for both functions is 'max(iterable)' and 'min(iterable)'.
Find Min & Mix in a list
If you have a list of integers, for example, you can use max() to get the largest integer in the list and min() to find the fewest number of lists:
l=eval(input("Enter a list of numbers")) # [4,7,9,10,45,21,46,67,23] --- input print("min color: #007020;">min(l)) print("max color: #007020;">max(l))
Min & Max of the list using a loop
If you don’t wish to use the pre-defined min() and max() functions, you can get the same desired result by writing a few lines of code using a for loop and iterating over the whole list manually to find the largest and smallest value.
Here is the algorithm for this solution:
- Keep the first item on the list in a variable called large for the max value and small for the min value.
- Now, cycle through the list of elements and compare them to one another. Assign the current element to small if it is less than small. You now have the new minimum value.
- Similarly, cycle through the list elements and compare them to one another. Assign the current element to large if it is greater than large. You now have the new maximum value.
- Repeat the preceding steps until you reach the end of the list. The actual minimum and maximum value of the string will be stored in the last value of a small and large variables.
- Return large and small respectively.
l=eval(input("Enter a list of numbers")) # [4,7,9,10,45,21,46,67,23] --- input larg = l[0] # initialize with the first value small =l[0] # initialize with the first value for num in l: if num > larg: larg = num if num small: smallest = num print("Largest:", larg) # Output: Largest: 9 print("Smallest:", small) # Output: Smallest: 1
Finding Max & Min between 2 lists
If you are given two or more lists and you have to find the largest and smallest element in all of them, then we have two different ways to solve this problem.
We can use the ‘+’ operator to first combine all the given lists and then apply the max() or min() function to find the desired result.
l1=eval(input("Enter the list1 of numbers")) l2=eval(input("Enter the list2 of numbers")) l3=eval(input("Enter the list3 of numbers")) # l1=[4,6 ,9,35,12,23,10] # l2=[0,9,29,56,23,56,18] # l3=[1,2,3,4,5,6,7,8,9,10] max_all = max(l1+l2+l3) min_all = min(l1+l2+l3) print ("The maximum of all 3 lists is : " + str(max_all)) print ("The minimum of all 3 lists is : " + str(min_all))
The maximum of all 3 lists is : 56 The minimum of all 3 lists is : 0
Or we can use the chain() function, which is faster than that in computation time, to combine the lists.
from itertools import chain l1=eval(input("Enter the list1 of numbers")) l2=eval(input("Enter the list2 of numbers")) l3=eval(input("Enter the list3 of numbers")) # l1=[4,6 ,9,35,12,23,10] # l2=[0,9,29,56,23,56,18] # l3=[1,2,3,4,5,6,7,8,9,10] max_all = max(chain(l1,l2, l3)) min_all = min(chain(l1,l2,l3)) print ("The maximum of all 3 lists is : " + str(max_all)) print ("The minimum of all 3 lists is : " + str(min_all))
The maximum of all 3 lists is : 56 The minimum of all 3 lists is : 0
Conclusion
So in this article, we covered the built-in Python functions: min() and max() as well as created a new function to discover the minimum and maximum values in a list using a loop. Happy Learning 🙂
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.