Can anyone help me clarify a concept? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th Jul 2019, 9:34 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
16 Answers
+ 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
29th Jul 2019, 12:02 PM
Tibor Santa
Tibor Santa - avatar
28th Jul 2019, 10:12 AM
Clueless Coder
Clueless Coder - avatar
+ 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
28th Jul 2019, 4:48 PM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 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.
28th Jul 2019, 2:54 PM
Tibor Santa
Tibor Santa - avatar
+ 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))
28th Jul 2019, 4:05 PM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 2
https://code.sololearn.com/cf6Pr0wrE60V/?ref=app Default, you can also watch this code
28th Jul 2019, 5:27 PM
TeaCoffeeCup
TeaCoffeeCup - avatar
+ 2
Thanks for helping me strengthen my lambda concept guys 😀 Now I have enough to use it in programs.
29th Jul 2019, 11:35 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 2
Rafikov what are those arguments used for?
29th Jul 2019, 11:37 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 1
https://code.sololearn.com/cXqo37wFan3L/?ref=app Here is the code with a little description for other questions ask me in comments
28th Jul 2019, 3:53 PM
TeaCoffeeCup
TeaCoffeeCup - avatar
+ 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
28th Jul 2019, 4:27 PM
Tibor Santa
Tibor Santa - avatar
+ 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/
28th Jul 2019, 4:59 PM
Tibor Santa
Tibor Santa - avatar
+ 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 .
28th Jul 2019, 9:40 PM
Anil
+ 1
Anil, if you write d1 = lambda..... Lambda is no anonymous anymore
29th Jul 2019, 5:22 AM
TeaCoffeeCup
TeaCoffeeCup - avatar
+ 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
29th Jul 2019, 11:34 AM
Zamraz Arif Khan
Zamraz Arif Khan - avatar
+ 1
Ternary means that you have only three arguments
29th Jul 2019, 11:36 AM
TeaCoffeeCup
TeaCoffeeCup - avatar
0
Default, first is here 1 if smth=1 else 2 ^ ^^^^^^^ ^ 1. 22222. 3
29th Jul 2019, 11:47 AM
TeaCoffeeCup
TeaCoffeeCup - avatar