Python 3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. #defining an anonymous value (int) >>> print(2**10) 1024 >>> x = 2**10 >>> print(x) 1024 #defining an anonymous function and calling it for a given input >>> (lambda x:x+2)(6) 8 #conditional expression >>> z = 3 if 3> 2 else 2 >>> z 3 # "for ..." is a statement and not an expression >>> z = for i in range(10): SyntaxError: invalid syntax #assignment (x=2) is not an expression >>> z = (x = 2) SyntaxError: invalid syntax #creating a named-function (similar to using: "def ...") >>> plus2 = lambda x: x+2 >>> plus2 at 0x000000000355F3C8> >>> def plus2(x): return x+2 >>> plus2(6) 8 >>> plus2 #An example of a function that returns a function - two methods for defining it >>> def make_pow(n): return lambda x: pow(x,n) >>> sqr = make_pow(2) >>> sqr at 0x00000000035632C8> >>> sqr(10) 100 >>> cub = make_pow(3) >>> cub(10) 1000 >>> def make_pow(n): def fixed_exp_pow(x): return pow(x,n) return fixed_exp_pow >>> cub = make_pow(3) >>> cub(10) 1000 #defining another name for the function len >>> x = len >>> x([1,2,3]) 3 #An example of a function that recieves a function as an argument >>> lst = ["amir", "yael", "michal", "benny"] >>> sorted(lst, key = lambda s: len(s)) ['amir', 'yael', 'benny', 'michal'] >>> sorted(lst, key = len) ['amir', 'yael', 'benny', 'michal'] >>> sorted(lst, key = lambda s: s[::-1]) ['michal', 'yael', 'amir', 'benny'] >>> sorted(["3", "111", "22"], key = int) ['3', '22', '111'] >>> sorted(["3", "111", "22"]) ['111', '22', '3'] #An example of a function that gets a function as an argument and returns a function >>> def compose(f1, f2): return lambda x: f1(f2(x)) >>> h1 = compose(lambda x:x+1, lambda x:2*x) >>> h1(5) 11 >>> h2 = compose(lambda x:2*x, lambda x:x+1) >>> h2(5) 12 #An anonymous function that returns a function >>> f = lambda x: (lambda y: x+y) >>> f at 0x0000000003563748> >>> g = f(3) >>> g at 0x00000000035637C8> >>> g(2) 5 >>> f(3)(2) 5 >>> #getting to know the * for defining a method that recieves 0 or more parameters #in our example params is a tuple # an example that we did not see in class! #---------------------------------------------------------------------------- >>> def mult(*params): result = 1 print(params) print(*params) for elem in params: result *= elem return result >>> mult(3,4) (3, 4) 3 4 12 #In our example t is a tuple and the content of t can be # passed to a function as a set of parameters using *t >>> t = (3,4,5) >>> *t SyntaxError: can use starred expression only as assignment target >>> mult(*t) (3, 4, 5) 3 4 5 60 >>> #---------------------------------------------------------------------------- >>> def info(lst, func, *params): return func(lst, *params) >>> lst = [10, 12, 14, 13, 11] >>> print(info(lst, (lambda l: sum([e for e in l if e>12])))) 27 >>> print(info(lst, (lambda l: [e for e in l if e>12]))) [14, 13] >>> print(info(lst, (lambda l,i: sorted(l)[i-1]), 2)) 11 >>> print(info(lst, (lambda l,i,j: l[i] if i>j else l[j]), 2, 0)) 14