Python Program to find Smallest Number in a List

Write a Python Program to find the Smallest Number in a List with a practical example.

Python Program to find the Smallest Number in a List Example 1

The min function returns the minimum value in a List.

# Example

a = [10, 50, 60, 80, 20, 15]

print("The Smallest Element in this List is : ", min(a))

The smallest list number output

The Smallest Element in this List is : 10

Python Program to find the Smallest Number in a List Example 2

This python program is the same as above. But this time, we are allowing the user to enter the length of a List. Next, we used For Loop to add numbers to the Python list.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

print("The Smallest Element in this List is : ", min(NumList))
Python Program to find Smallest Number in a List 2

Using sort function Example 3

The sort function sorts List elements in ascending order. Next, we are using Index position 0 to print the first element in a List.

a = [100, 50, 60, 80, 20, 15]

a.sort()
print("The Smallest Element in this List is : ", a[0])

The minimum list number using sort function output

The Smallest Element in this List is : 20

Smallest Number in a List Example 4

This list smallest number is the same as above. But this time, we allow users to enter their list items.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()
print("The Smallest Element in this List is : ", NumList[0])

Output

Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 7
Please enter the Value of 2 Element : 9
Please enter the Value of 3 Element : 22
Please enter the Value of 4 Element : 90
Please enter the Value of 5 Element : 5
Please enter the Value of 6 Element : 67
The Smallest Element in this List is :  5

Python Program to find the Smallest Number in a List using for loop

In this program, we are not using any built-in function such as the sort or min function.

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

smallest = NumList[0]    
for j in range(1, Number):
    if(smallest > NumList[j]):
        smallest = NumList[j]
        position = j

print("The Smallest Element in this List is : ", smallest)
print("The Index position of the Smallest Element is : ", position)

Output

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 223
Please enter the Value of 2 Element : 43
Please enter the Value of 3 Element : 22
Please enter the Value of 4 Element : 67
Please enter the Value of 5 Element : 54
The Smallest Element in this List is :  22
The Index position of the Smallest Element is :  2

From the above Program example, the User inserted values are
NumList[5] = {223, 43, 22, 67, 54}
smallest = NumList[0] = 223

First Iteration – for 1 in range(1, 5) – Condition is true
So, it starts executing If statement inside the loop until the condition fails.

if (smallest > NumList[j]) inside the for loop is True because (223 > 43)
smallest = NumList[1]
smallest = 43
position = 1

Second Iteration: for 2 in range(1, 5) – Condition is true
If (smallest > NumList[2])  =  (43 > 22) – Condition True
smallest = NumList[2]
smallest = 22
Position = 2

Third Iteration: for 3 in range(1, 5) – Condition is true
If (smallest > NumList[3])  =  (22 > 67) – Condition False
smallest = 22
Position = 2

Fourth Iteration: for 4 in range(1, 5) – Condition is true
If (22 > 54) – Condition False
smallest = 22
Position = 2

Fifth Iteration: for 5 in range(1, 5) – Condition is False
So, it exits from the loop.