In this article, we will learn to find the smallest number in a list using the python program.
We find the smallest number in a list using 3 different methods:
- Using min() function
- By comparing each element
- By using sort() function
Using min() function
min() is the in-built function of the python, which returns the smallest value of the list.
Explanation
- First, we declared the list.
- Then, we use the min() function to find the smallest number in a list.
- Then we print the small value.
Program
1
2
3
4
5
6
7
8 | # lsit
list = [11, 10, 13, 15, 28]
# find smallest elements using min() function
small = min(list)
# print smallest element
print("The smallest element of a list is:",small)
|
Output
The smallest element of a list is: 10
By comparing each element
In this method, we will iterate the list and compare each element to find the smallest number.
Explanation
- First, we declared the list and a small variable.
- And, we assume the first elements as a small number and assign them to the small variable.
- Then we run the for loop and check each element of the loop is smaller than small or not.
- If any element is smaller than the small variable then we update the value of the small variable.
- Then, lastly, we prin the small variable as the smallest number.
Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | # lsit
list = [11, 10, 13, 15, 28]
# assume the first element is small
small = list[0]
# now iterate the list
for i in range(0,len(list)):
# check the all elements is less than small or not
if list[i] < small:
# if the elements is smaller than small than update the value of small
small = list[i]
# print smallest element
print("The smallest element of a list is:",small)
|
Output
The smallest element of a list is: 10
Using sort() function
sort() function used to arrange a list in ascending order. so, using the sort() function we will easily find the smallest number value from the list.
Explanation
- 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 first elements are always smaller.
- So, we print the first element.
Program
1
2
3
4
5
6
7
8
9
10
11 | # lsit
list = [11, 10, 13, 15, 28]
# arrange the list in ascending order
list.sort()
# so, the first elements of ascending list is small
small = list[0]
# print smallest element
print("The smallest element of a list is:",small)
|
Output
The smallest element of a list is: 10