Find Minimum of Two Numbers


Find Minimum of Two Numbers

There are many ways to find out the minimum of given two numbers. In this tutorial, we will learn three ways of finding out the minimum of two numbers.

https://youtu.be/uJ4nqz3nj_4?si=nTazs_xsxP6EmKOg

1. Find Minimum using min() builtin function

To find the minimum of given two numbers in Python, call min() builtin function and pass the two numbers as arguments. min() returns the smallest or the minimum of the given two numbers.

The syntax to find the minimum of two numbers: a, b using min() is

min(a, b)

In the following program, we take numeric values in two variables a and b, and find out the minimum of these two numbers using min().

Python Program

a = 7
b = 52
minimum = min(a, b)
print(f'Minimum of {a}, {b} is {minimum}')

Output

Minimum of 7, 52 is 7

2. Find Minimum Value using If Statement

In the following program, we will use simple If statement to find the minimum of given two numbers.

Python Program

a = 7
b = 52

minimum = a
if b > a :
    minimum = b

print(f'Minimum of {a}, {b} is {minimum}')

Output

Minimum of 7, 52 is 7

3. Writing our own Lambda Function to find Minimum Value

We can also write a lambda function to find the minimum of two numbers. We use Ternary Operator inside lambda to choose the minimum value.

In the following program, we write a lambda function that can return the maximum of given two numbers.

Python Program

a = 7
b = 52
minimum = lambda a, b: a if a < b else b
print(f'Minimum of {a}, {b} is {minimum(a, b)}')

Output

Minimum of 7, 52 is 7