def process_three(a, b, c, torun): torun(a) torun(b) torun(c) def print_thrice(x): print(x, x, x) def half_and_print(x): print(x/2) process_three(2, 3, 4, print_thrice) process_three(6, 3, 10, half_and_print) process_three("dog", "fox", "coyote", print_thrice) def list_results(a, b, c, f): return f(a), f(b), f(c) from math import * print(list_results(pi, 0, 2*pi, cos)) # This is not a good idea def run_other(p, f): return p, f(p, f) def safe_function_for_run_other(p, f): print("safe_function_for_run_other: We have paramterers ", p, " and ", f) if safe_function_for_run_other == f: print("We were passed ourself!") return p run_other(100, safe_function_for_run_other) # this is the bad idea part # run_other(100, run_other) def select_trig_function(): function = input("Enter a trig function: ") if "sin" in function.lower(): return sin if "cos" in function.lower(): return cos if "tan" in function.lower(): return tan print("Sorry, we don't recognize that!") def sorry_function(x): print("Sorry, we can't do anything with ", x) return sorry_function def select_run_trig(n): trig_function = select_trig_function() print("Running on ", n) print("Result: ", trig_function(n)) select_run_trig(pi) select_run_trig(100) our_function = lambda x,y : x*y print(our_function(5, 6)) print( (lambda x,y : x/y)(10, 3)) def make_adder(): return lambda x,y : x+y print(make_adder()) print(make_adder()(9, 1200)) trigs = [sin, cos, tan, lambda x : x**30] for t in trigs: print(t(5)) print(trigs[3](9)) #def return_counter(): Doesn't work # count = 0 # def counter(): # global count # count += 1 # return count # return counter # oc = return_counter() # print(oc()) def return_counter(): count = 5 def counter(): return count return counter oc = return_counter() print(oc())