Q:

Find small number between two numbers using Lambda function in Python

belongs to collection: Python Lambda Function Programs

0

Given two numbers (ab), we have to find the smallest number.

Example:

Input:
a = 10, b = 8

Output: 8

Input: 
a = 20, b = -20

Output: -20

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python code to find small number using Lambda function and min() function

# Using Lambda function and min() function

small = lambda a, b : min(a,b)

print(small(20, -20))
print(small(10, 8))
print(small(20, 20))

Output:

-20
8
20

Method 2: Using Lambda Expression and Ternary Operator

Pass the numbers (a and b) to the lambda function and compare them using the ternary operator.

Python code to find small number using Lambda function and ternary operator

# Using Lambda function and Ternary Operator

small = lambda a, b : a if a < b else b

print(small(20, -20))
print(small(10, 8))
print(small(20, 20))

Output:

-20
8
20

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Find the number occurring odd number of times usin... >>
<< Intersection of two arrays using Lambda expression...