Question related to lambda | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Question related to lambda

x='cyzxyz' (lambda x : print(x))(x) O.p - cyzxyz Why there an x outside of print statement ? Could anyone explain the second line pls

4th Aug 2022, 4:15 PM
Levi
Levi - avatar
3 Answers
+ 3
x = 'cyzxyz' (lambda x : print(x))(x) ^ parameter of the function (lambda x : print(x))(x) ^ function "body", x refers to the parameter (lambda x : print(x))(x) ^ argument that you pass to the function; x refers to the variable x, that holds the string You could rewrite the code like this: y = 'somestring' (lambda x : print(x))(y) or def fun(x): print(x) fun(y)
4th Aug 2022, 5:23 PM
Lisa
Lisa - avatar
+ 3
Again we can rewrite the code to make it clearer: a=[lambda x=num:x*10 for num in range(10)] for fun in a: print(fun()) Here num is the default value for the x parameter
4th Aug 2022, 5:54 PM
Lisa
Lisa - avatar
0
Lisa Thanks u so much for ur explanation. And I have one more query a=[lambda x=x:x*10 for x in range(10)] for i in a: print(i()) What does lambda x=x mean ? Could u explain in simple terms pls
4th Aug 2022, 5:37 PM
Levi
Levi - avatar