Python – Smallest of Three Numbers

Python – Find Smallest of Three Numbers

To find the smallest of three numbers, we could write a compound condition to check if a number is less than other two.

Example 1: Find Smallest of Three Numbers using IF

In this example, we shall use simple Python If statement to find the smallest of three numbers.

We shall follow these steps to find the smallest number of three.

  1. Read first number to a.
  2. Read second number to b.
  3. Read third number to c.
  4. If a is less than b and a is less than c, then a is the smallest of the three numbers.
  5. If b is less than a and b is less than c, then b is the smallest of the three numbers.
  6. If c is less than a and c is less than b, then c is the smallest of the three numbers.

Python Program

a = int(input('Enter first number  : '))
b = int(input('Enter second number : '))
c = int(input('Enter third number  : '))

smallest = 0

if a < b and a < c :
    smallest = a
if b < a and b < c :
    smallest = b
if c < a and c < b :
    smallest = c

print(smallest, "is the smallest of three numbers.")

Output

Python - Smallest of Three Numbers

Example 2: Find Smallest of Three Numbers using IF-ELIF

In our previous example, we have written conditions to find the smallest. But, these conditions, we have written are independent of each other. And we have not taken advantage that if a is not less than either of b or c, then there is no use to check if b is less than a or c is less than a.

Also, if neither a nor b is the smallest, then it is evident that c is the smallest.

So, using elif statement, we shall take advantage of the dependency of conditions.

Python Program

a = int(input('Enter first number  : '))
b = int(input('Enter second number : '))
c = int(input('Enter third number  : '))

smallest = 0

if a < b and a < c :
    smallest = a
elif b < c :
    smallest = b
else :
    smallest = c

print(smallest, "is the smallest of three numbers.")

Output

Python - Smallest of Three Numbers

Summary

In this tutorial of Python Examples, we learned how to find the smallest of three numbers using conditional statements.