1

I am a new to programming, and was looking for some help with adding multiple expressions to a lambda function. But cannot seem to get to work. It's a noob level problem, so can please some one help me out?

a = int()
b = int()

greater (a,b) = lambda(a,b): (a > b) == 'a' or (b > a) == 'b' :

print (greater(10,9))
DeepSpace
  • 65,330
  • 8
  • 79
  • 117
neo_jr
  • 17
  • 3
  • 1
    Keep in mind this violates the Python style guide (PEP8). At this point you might as well define this function with `def` – DeepSpace Aug 12 '20 at 18:54
  • 1
    hi, i understand def is the way to go. I was just going through the concept of lambda functions, and wanted to perform a simple operation. – neo_jr Aug 12 '20 at 18:56
  • [https://docs.python.org/3/reference/expressions.html#lambda](https://docs.python.org/3/reference/expressions.html#lambda) – wwii Aug 12 '20 at 19:05
  • See also [Is there a way to perform “if” in python's lambda](https://stackoverflow.com/questions/1585322/is-there-a-way-to-perform-if-in-pythons-lambda) – user2314737 Aug 12 '20 at 19:23

1 Answers1

3

You can achieve this using an if condition (which will not "catch" the case a and b are equal):

greater = lambda a, b: 'a' if a > b else 'b'

Catching the equality case is a bit tricker and requires nested if:

greater = lambda a, b: 'a' if a > b else 'b' if b > a else 'N/A'

(Note that there are no parenthesis after greater and around the lambda's arguments)

However, this violates the Python style guide (PEP8). At this point (where you already named the lambda), you should just define this function with def.

DeepSpace
  • 65,330
  • 8
  • 79
  • 117
  • 1
    I'd rather specify this as a `ternary operation` of some sorts... a traditional if statement does not operate this way and doesn't return a value – M Z Aug 12 '20 at 18:59
  • hi i just executed your expression, but it's still giving me a syntax error. ok I'll tell you the actual question.Create a lambda function 'greater', which takes two arguments x and y and return x if x>y otherwise y. – neo_jr Aug 12 '20 at 19:01
  • in the second case I would use brackets, otherwise it is not obvious which condition executes first – Dan Aug 12 '20 at 19:01
  • 1
    @Dan Since this piece of code shouldn't be used anyway, I don't think that really matters – DeepSpace Aug 12 '20 at 19:03
  • @MZ I'm not sure what you mean by "a traditional if statement does not operate this way and doesn't return a value" – DeepSpace Aug 12 '20 at 19:04
  • @DeepSpace can you help me out with this question. Create a lambda function 'greater', which takes two arguments x and y and return x if x>y otherwise y. what would coding look like ? – neo_jr Aug 12 '20 at 19:05
  • @neo_jr that is almost exactly what the code in this answer is doing, except that it returns the literal strings `'a'` and `'b'` (instead of `a` and `b`, respectively. It should be straightforward what needs to be changed – DeepSpace Aug 12 '20 at 19:07
  • @MZ I'm aware of the differences, just don't understand what you meant by saying that `if` conditions don't "operate this way". What does "this" refer to? – DeepSpace Aug 12 '20 at 19:08
  • @DeepSpace if statements are for branching, the expression of an if statement does not return a value – M Z Aug 12 '20 at 19:08
  • @MZ Try this code, it works as intended... BTW, as for your last suggestion, at this point you can just do `greater = max` but clearly that is not the intention – DeepSpace Aug 12 '20 at 19:09
  • @DeepSpace ok. https://docs.python.org/3/reference/expressions.html#conditional-expressions – M Z Aug 12 '20 at 19:15
  • @DeepSpace yes, it that lambda form and it was not sufficing my needs. like you have suggested, i removed the quotes, and it did work. Also, can you please give me hint, as to why the else 'N/A' was necessary, because i removed it and ran the code, but it gave a syntax error. – neo_jr Aug 12 '20 at 19:28