Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. # define a lambda function and check its type (function, daaah) >>> type(lambda x:x+2) # define a lambda function and immediately call it with a parameter >>> (lambda x:x+2)(6) 8 >>> y=3 >>> (lambda x:x+y)(6) 9 # define a lambda function and give it a name; equivalent to "def f(): ..." >>> f = (lambda x:x+y) >>> f(3) 6 # example 1 >>> def make_pow(n): return lambda x:pow(x,n) >>> make_pow(2) . at 0x102450f80> >>> sqr = make_pow(2) >>> sqr(3) 9 >>> cub = make_pow(3) >>> cub(3) 27 >>> make_pow(2)(3) 9 >>> type(sqr) # example 2 >>> sorted(["red","lefty","poncho","dafna"]) ['dafna', 'lefty', 'poncho', 'red'] >>> sorted(["red","lefty","poncho","dafna"],key=len) ['red', 'lefty', 'dafna', 'poncho'] >>> sorted(["red","lefty","poncho","dafna"],key=lambda s:len(s)) ['red', 'lefty', 'dafna', 'poncho'] >>> sorted(["red","lefty","poncho","dafna"],key=lambda s:s[::-1]) ['dafna', 'red', 'poncho', 'lefty'] >>> sorted(["romeo","red","almo"],key=lambda s:s[0]) ['almo', 'romeo', 'red'] >>> def first_char(s): return s[0] >>> sorted(["romeo","red","almo"],key=first_char) ['almo', 'romeo', 'red'] >>> sorted(["3","222","11"],key=int) ['3', '11', '222'] # example 3 >>> def compose(f1,f2): return lambda x:f1(f2(x)) >>> h1 = compose(lambda x:x+1,lambda x:2*x) >>> h1(3) 7 >>> h1 = compose(lambda x:2*x,lambda x:x+1) >>> h1(3) 8 # example 4 >>> def info(lst,func,*params): return func(lst,*params) >>> info([10,12,14,13,11],lambda l: sum([e for e in l if e>12])) 27 >>> info([10,12,14,13,11],lambda l: [e for e in l if e>12]) [14, 13] >>> info([10,12,14,13,11],lambda l,i: sorted(l)[i-1],2) 11 >>> info([10,12,14,13,11],lambda l,i,j: l[i] if j