belongs to collection: Python Lambda Function Programs
Given two numbers (a, b), we have to find the smallest number.
Example:
Input: a = 10, b = 8 Output: 8 Input: a = 20, b = -20 Output: -20
# 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
Pass the numbers (a and b) to the lambda function and compare them using the 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))
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Python code to find small number using Lambda function and min() function
Output:
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
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer