I'm pretty new to Python (and programming in general). I was wondering, since lambda and functions are very similar, when is it proper to use which and what are the differences between them?
The reason I'm asking is I've only seen lambda used for very basic and simple programs, such as:
sq = lambda x: x**2
print(sq(25))
While functions can be much more complicated like having multiple parameters, different looping types, if/else, recursive, calling another function (composition, I think), etc.
I know you can call a function inside a lambda like:
def turnUppercase(n):
return n.upper()
a = lambda x: turnUppercase(x)
print(a('Cookie'))
That example is pointless, but still... I've never tested the limits of lambda by trying other things.
What are the limits of lambda? How can you extend the functionalities of lambdas (if only to impress people) to match that of functions? (Calling a function inside lambda, calling another lambda, loops inside, and so on).
Note I'm asking about Python 3.
Thanks!
def). Otherwise uselambdamy_fun = lambda x: x + 1for example and that's the same asdef my_fun(x): return x +1. Really it's just a matter of convenience and readability though lambdas are limited in what you can express: docs.python.org/3.1/reference/expressions.html#lambda. In particular they can't contain statementsdef.