2.2. Lambda Expressions - JulTob/Python GitHub Wiki

Anonymous functions.

When you get it, you ♥ love ♥ it.

lambda x: 3*x + 1


g = lambda x: 3*x+1
g(2)  # 7

full_name = lambda fn, ln : fn.strip().title() + " " + ln.strip().title()
full_name("    leonhard ", "EULER")


SciFi = ["Isaac Asimov", "H.G. Wells", "Julius Verne", "Douglas Adams"]
help(SciFi.sort)
>>> L.sort(key = None, reverse = False)
SciFi.sort( key = lambda name: name.split(" ")[-1].lower() )


def build_quadratic_function(a,b,c):
   """Returns the function f(x) = ax^2 + bx + c"""
   return lambda x: a*x**2 + b*x + c
f = build_quadratic_function(2,3,-5)
f(0)
>>> -5