Python Lambdas: Can we create a lambda function which involves more than one variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python Lambdas: Can we create a lambda function which involves more than one variable?

A basic implementation of python lambda is: print((lambda x: x**2 + 5*x + 4) (-4)) When I form an expression like this, what happens is: print((lambda x,y: x^4 - y^4 + 4*x*y)(-4)(-5)) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> print((lambda x,y: x^4 - y^4 + 4*x*y)(-4)(-5)) TypeError: <lambda>() missing 1 required positional argument: 'y' How do I use lambda for more than one variable?

23rd Feb 2019, 5:20 PM
Pranav Gadre
Pranav Gadre - avatar
5 Answers
+ 2
Yes. Just put all arguments in a tuple 👌
23rd Feb 2019, 5:49 PM
Anna
Anna - avatar
+ 5
You're welcome ☺️👍
23rd Feb 2019, 7:43 PM
Anna
Anna - avatar
+ 4
print((lambda x,y: x^4 - y^4 + 4*x*y)(-4, -5))
23rd Feb 2019, 5:45 PM
Anna
Anna - avatar
+ 1
Thanks a lot! so, if an equation involves n number of variables, I can put it in the above format & get the code run, right?
23rd Feb 2019, 5:47 PM
Pranav Gadre
Pranav Gadre - avatar
+ 1
Thanks a lot for your prompt responses. Very rare across the internet. This makes the Sololearn community, one of it's kind!
23rd Feb 2019, 7:24 PM
Pranav Gadre
Pranav Gadre - avatar