#lambda.py # multiple arguments g = lambda x,y: 3*x+1 + 2*y+10 #return one number print(g(1,2)) g = lambda x,y: (3*x+1,2*y+10) #return tuple print(g(1,2)) # list argument g = lambda x: [i**2 for i in x] #return list print(g([1,2,3,4])) arithmetic_mean = lambda x: sum(x)/len(x) print(arithmetic_mean([1,2,3,4])) def product(nums): prod = 1 for num in nums: prod *= num return prod import math geometric_mean = lambda x: math.pow(product(x) , 1/len(x)) print(geometric_mean([1,2,3,4])) harmonic_mean = lambda x: len(x) / sum([1/i for i in x]) print(harmonic_mean([1,2,3,4])) # sorting s = "asdfBqweZrty" print(sorted(s, key=lambda c: c.lower())) #case-insensitive #return sub_string of s and +/- surrounding find = lambda s,subS,sur: s[s.find(subS)-sur:s.find(subS)+len(subS)+sur] if subS in s else None alice = ''' Alice was beginning to get very tired of sitting by her sister on the bank and of having nothing to do once or twice she had peeped into the book her sister was reading but it had no pictures or conversations in it and what is the use of a book thought Alice without pictures or conversation''' print(find(alice.replace('\n',' '),'sister',3))