How To Find Smallest Number In a List in Python
Posted on July 6, 2023  (Last modified on July 11, 2023 )
3 minutes • 499 words
Table of contents
How to find the lowest number in a list in python
In this short tutorial we will learn about how to find the lowest number on a list using a few different methods.
Why find the lowest number in a list?
Finding the lowest number in a list is a common thing for beginners to do to practice more about coding. Finding the lowest number in a list in Python is helpful when you want to know the smallest value in a group of numbers. It’s useful for tasks like data analysis, comparisons, and making decisions based on the minimum value.
There are multiple ways for us to find the lowers number on a list :
- Using min() function
- Using a for loop
- Using sort() method
Finding lowest number with min()
The min() function is a built-in function in Python that returns the smallest item from an iterable or a series of arguments. When used with a list, it can quickly determine the minimum value without the need for a loop or sorting. Here’s an example:
num_list = [5, 3, 8, 4, 7]
min_num = min(num_list)
print(min_num)
Output:
3
Finding lowest number with for loop
We will use for loop to find the lowest number, for example:
num_list = [5, 3, 8, 4, 7]
min_num = num_list[0]
for num in num_list:
if num < min_num:
min_num = num
print(min_num)
Output:
3
Explanation: In this example, we start by assuming that the first element of the list (my_list[0]) is the minimum value. Then, we iterate through each element of my_list using a for loop. If a number is found that is smaller than the current minimum (num < min_num), we update the min_num variable with that number. Finally, the minimum value is printed to the console.
Finding lowest number in a list with sort()
The sort() method is a built-in method in Python that sorts the list in ascending order. After sorting the list, the smallest number will be located at the beginning of the sorted list, and we just need to print the first number on the list. Here’s an example
num_list = [5, 3, 8, 4, 7]
num_list.sort()
min_num = num_list[0]
print(num_list)
print(min_num)
Output:
[3, 4, 5, 7, 8]
3
Conclusion:
In conclusion, there are multiple ways to find the lowest number in a list in Python. One method is using the min() function, which quickly determines the minimum value without the need for a loop or sorting. Another method is using a for loop to iterate through the list and update the minimum value if a smaller number is found. The third method involves sorting the list using the sort() method and printing the first number. These methods offer flexibility based on specific requirements and preferences.
Share
Other Tutorial
Read Previous | Read Next |
---|---|
How To Find The Biggest Number In A List In Python | How To Calculate Average From A List In Python |