educative.io

Usage of lamba functions s not clear

def calculator(operation, n1, n2):

return operation(n1, n2)  # Using the 'operation' argument as a function

10 and 20 are the arguments.

result = calculator(lambda n1, n2: n1 - n2, 10, 20)

The lambda multiplies them.

print(result)

print(calculator(lambda n1, n2: n1 / n2, 10, 20))

Can we give the operator also as an argu

Hi @Agrita_K

Can you please provide the lesson link?

Thank you

Hi @Agrita_K ,
Yes, we can pass the operator in char data type as an argument but for this case, we have to write lambda function accordingly.
For example
operations = {
“+”: lambda x, y: x + y,
“*”: lambda x, y: x * y
}
operations[operator](int(num1), int(num2))