+ 4
Can anyone help me clarify a concept?
I am trying to make lambda do a mathematical operation and then to return it to a string but it's not working. Can anyone help me? Your help will be greatly appreciated. Thank you https://code.sololearn.com/c6SfDvBFOzei/?ref=app
16 Réponses
+ 1
Default to your question about nesting ternary operators, check this thread especially the answer of swim
https://www.sololearn.com/Discuss/1909347/?ref=app
+ 3
Thanks now my code works 😀
How can boolean statements be carried out in a lambda function the way you did in your program?
Nice program btw👍
https://code.sololearn.com/c6SfDvBFOzei/?ref=app
+ 2
A lambda expression is an anonymous function. You can write out any lambda as a normal function (with def). Or you can assign the lambda expression to a variable, but then it is not really anonymous anymore, essentially it is the same as def but with fewer lines.
Lambda is useful mostly when you need to pass a function as argument of a higher order function, for example map, filter or reduce.
+ 2
I have some questions
1. How are people able to make one liners with this?
2. After the anonymous function has completed its work, can it be turned into a str?
E.g
print(str(lambda x : x + 5,5))
+ 2
https://code.sololearn.com/cf6Pr0wrE60V/?ref=app
Default, you can also watch this code
+ 2
Thanks for helping me strengthen my lambda concept guys 😀
Now I have enough to use it in programs.
+ 2
Rafikov what are those arguments used for?
+ 1
https://code.sololearn.com/cXqo37wFan3L/?ref=app
Here is the code with a little description for other questions ask me in comments
+ 1
If you pass a numerical value to print, in python it is automatically turned into string so you don't need to use str.
However you have to put the lambda expression in parentheses, and afterwards the parameters as well, similar to a normal function call. These will both work:
print((lambda x: x+5)(7))
print((lambda x: x+5)(int(input())))
In one-liners, lambda can be used to pass a series of arguments (like a list) to a function, or to manage a recursive call like here:
https://code.sololearn.com/cmns3OFIx57C/?ref=app
+ 1
I guess I used two types of conditions in the quicksort code:
Ternary operators, is just a compact shortcurt to do an if-then-else block in a single expression.
http://book.pythontips.com/en/latest/ternary_operators.html
And the other type was a conditional list comprehension where you 'filter' which elements are included in the list.
https://treyhunner.com/2015/12/JUMP_LINK__&&__python__&&__JUMP_LINK-list-comprehensions-now-in-color/
+ 1
First it will carryout mathematical function and then converts output to string for understanding use as below,
d1 = str((lambda x : x + 5)(5))
print(d1)
Here if you check type(d1) gives output is string .
+ 1
Anil, if you write d1 = lambda.....
Lambda is no anonymous anymore
+ 1
Is it possible to do a ternary operation with an elif statement?
I tried it, it doesn't work or maybe I'm not doing it correctly
+ 1
Ternary means that you have only three arguments
0
Default, first is here
1 if smth=1 else 2
^ ^^^^^^^ ^
1. 22222. 3