Minimum of Two Numbers

Write a Python lambda function that returns the minimum of two numbers. The function should accept two numbers and return the smaller one.

Example 1:

Input: 5, 10
Output: 5

Example 2:

Input: -7, -3
Output: -7

You can use Python’s built-in min function or a conditional expression to get the smaller number.

# Define the lambda function
min_two_numbers = lambda x, y: x if x < y else y

# Test the function
print(min_two_numbers(5, 10))  # Output: 5
print(min_two_numbers(-7, -3))  # Output: -7

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!