Search code examples
python-3.xlambda

How to print lambda function arguments when lambda is called in Python3


I have defined a lambda expression and I want as I use it later in my code to print the changing each time parameters within the lambda call. What should the print(lambda) look like?

x = lambda a,b,c: a+2*b+3*c

print(x(1,2,3))   
print(x(1,1,1))   
print(x(1,4,1))   
Desired sample output

14 for a=1, b=2, c=3

6 for a=1, b=1, c=1

12 for a=1, b=4, c=1


Solution

  • this lambda function will give you your desire output

    >>> x = lambda a,b,c: str(a+2*b+3*c)+" for a="+str(a)+" ,b="+str(b)+" ,c="+str(c)
    >>> print(x(1,2,3))
    14 for a=1 ,b=2 ,c=3