You are currently viewing Minimum of Two Numbers in Python

How to find the minimum of two numbers in Python? You can create a program that takes two integers as input from the user and returns the minimum of the two numbers. The easiest way to find the minimum of two numbers is by using the built-in min() function.

Advertisements

Alternatively, you can find a minimum of two numbers by using the if else statement, sorted(), lambda, and reduce() functions. In this article, I will explain how to find the minimum of two numbers by using all these functions with examples.

Related: You can also find the maximum of two numbers in Python.

1. Quick Examples of a Finding Minimum of Two Numbers

If you are in a hurry, below are some quick examples of how to find the minimum of two numbers.


# Quick examples of finding minimum of two numbers

# Initialize two variables
x = 25
y = 10

# Example 1: Find the minimum value 
# Using min() function
min_value = min(x, y)

# Example 2: Using If statement
# To find the minimum value
def minimum(x, y): 
    if x < y:
        return x
    else:
        return y 
min_value = minimum(x, y)

# Example 3: Find the minimum value 
# Using if else statement
if x < y:
    min_value = x
else:
    min_value = y

# Example 4: Using sorted() function
min_value = sorted([x, y])[0]

# Example 5: Using the lambda function
minimum = lambda x, y: x if x < y else y
min_value = minimum(x, y)

# Example 6: Find the minimum value 
# Using a ternary conditional operator
min_value = x if x < y else y

# Example 7: Find the minimum value 
# Using reduce() function
min_value = reduce(lambda a, b: a if a < b else b, [x, y])

2. Find a Minimum of Two Numbers Using the min()

You can find a minimum of two numbers using min() the function. For example, first, initializes two variables, x and y, with values of 25 and 10, respectively. Then, the min() function is used to find the minimum value between x and y. The resulting minimum value is stored in the min_value variable.


# Initialize two variables
x = 25
y = 10

# Find minimum value 
# Using min() function
print("First number:", x)
print("Second number:", y)
min_value = min(x, y)
print("The minimum value: ", min_value)

Yields below output.

3. Find a Minimum of Two Numbers Using if else Statement

You can also use an if else statement to find the minimum of two numbers. For example, the min() function takes two numbers x and y as input and compares them using an if statement. If the first number is less than the second number, it returns the first number as the minimum. Otherwise, it returns the second number as the minimum.

You can modify the values of x and y to test the function with different numbers. The result will be printed, indicating the minimum of the two numbers.


# Initialize two variables 
x = 25
y = 10

# Using If statement
def minimum(x, y): 
    if x < y:
        return x
    else:
        return y 
min_value = minimum(x, y)
print("The minimum value: ", min_value)

Yields the same output as above.

Similarly, you can also be finding the minimum value using an if statement. In this program, the if statement compares x and y. If x is less than y, it assigns the value of x to min_value. Otherwise, it assigns the value of y to min_value. Finally, it prints the minimum value.


# Initialize two variables 
x = 25
y = 10

# Find minimum value 
# Using if statement
if x < y:
    min_value = x
else:
    min_value = y
print("The minimum value: ", min_value)

Yields the same output as above.

4. Using sorted() Function

You can also find the minimum of two numbers using the sorted() function. For example, the sorted() function is used to sort the list[x, y] in ascending order. To find the minimum value, you access the element at the index 0 of the sorted list.

Note that this approach works for finding the minimum or maximum of any number of values, not just two.


# Initialize two variables
x = 25
y = 10

# Using sorted() function
# To find the minimum value
min_value = sorted([x, y])[0]
print("The minimum value:", min_value)

Yields the same output as above.

5. Using the Lambda Function

You can also find the minimum value between x and y using a lambda function within a single line. For instance, the lambda function takes two arguments x and y, and it returns x if x is less than y, otherwise, it returns y. The lambda function is then assigned to the variable minimum. By calling minimum(x, y), it evaluates and returns the minimum value between x and y.


# Initialize two variables 
x = 25
y = 10

# Using the lambda function
minimum = lambda x, y: x if x < y else y
min_value = minimum(x, y)
print("The minimum value:", min_value)

Yields the same output as above.

6. Using Ternary Operator

You can also use the ternary conditional operator to find the minimum of two numbers in Python. In this program, the ternary operator x if x < y else y is used to compare x and y. If x is less than y, the value of x is assigned to min_value. Otherwise, the value of y is assigned to min_value.

The ternary operator is a concise way to express conditional statements in a single line. It has the form x if condition else y, where x is the value if the condition is True, and y is the value if the condition is False. In this case, it helps us determine the minimum value between x and y in a compact manner.


# Initialize two variables
x = 25
y = 10

# Find minimum value 
# Using ternary conditional operator
min_value = x if x < y else y
print("The minimum value:", min_value)

Yields the same output as above.

7. Using reduce() Function

To find the minimum of two numbers using the reduce() function from the functools module. In this program, you import the reduce() function from the functools module. The reduce() function applies a function of two arguments cumulatively to the items of an iterable, from left to right, reducing it to a single value.

You can use a lambda function as the function argument for reduce(). The lambda function takes two arguments x and y, compares them using the if-else condition, and returns the minimum value. The reduce() function applies this lambda function to the iterable[x, y], reducing it to a single minimum value.


from functools import reduce

# Initialize two variables
x = 25
y = 10

# Find minimum value 
# Using reduce() function
min_value = reduce(lambda a, b: a if a < b else b, [x, y])
print("The minimum value:", min_value)

Yields the same output as above.

Conclusion

In this article, I have explained how to find the minimum of two numbers in Python by using the min(), if else statement, sorted(), lambda, and reduce() functions with examples.

Happy Learning !!