>>> def f(x): return x*3 ... >>> f(4) 12 >>> f = 14 >>> f(4) Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not callable >>> ## function names are variable names >>> ## functions are objects on the heap. >>> 5 5 >>> "cows" 'cows' >>> (lambda x: 3*x + 1)(5) 16 >>> (lambda : print("fooment"))() fooment >>> (lambda x, y, z : x + 2*y + 3*z)(1,2,3) 14 >>> cat = lambda x, y, z : x + 2*y + 3*z >>> cat(1,2,3) 14 >>> ^D MAC:Thu Mar 03:12:56:~> vi table_of_values.py MAC:Thu Mar 03:12:59:~> python table_of_values.py 5625 MAC:Thu Mar 03:12:59:~> !vi vi table_of_values.py MAC:Thu Mar 03:13:02:~> python table_of_values.py None MAC:Thu Mar 03:13:02:~> !vi vi table_of_values.py MAC:Thu Mar 03:13:02:~> !p python table_of_values.py
xf(x)
527
26
866
983
12146
02
MAC:Thu Mar 03:13:02:~> !vi vi table_of_values.py >>> for k, item in enumerate(x): ... print(f"x[{k}] = {x[k]}") ... x[0] = cat x[1] = dawg x[2] = elephant x[3] = ferret x[4] = goat >>> for k in enumerate(x): ... print(f"x[{k[0]}] = {k[1]}") ... x[0] = cat x[1] = dawg x[2] = elephant x[3] = ferret x[4] = goat >>> for k, item in enumerate(x, 50): ... print(k, item) ... 50 cat 51 dawg 52 elephant 53 ferret 54 goat >>> for k, item in enumerate("caterwaul"): ... print(f"{k} {item}") ... 0 c 1 a 2 t 3 e 4 r 5 w 6 a 7 u 8 l >>> ^D MAC:Thu Mar 03:13:14:loopy> python loopy.py **************** Problem 1 tests ************** FAIL because f([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], at 0x103770e50>]) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]. Failed Output: [] **************** Problem 2 tests ************** **************** Problem 3 tests ************** FAIL because f([[0, 12, 4, 51, 3, 9, 40], 20]) != 91. Failed Output: 0 **************** Problem 4 tests ************** FAIL because f([1, 5, 7, 9, 11, 13, 21, 25]) != True. Failed Output: False **************** Problem 5 tests ************** **************** Problem 6 tests ************** FAIL because f(['cat', 'elephant']) != cealtephant. Failed Output: **************** Problem 7 tests ************** FAIL because f([10, at 0x103770ee0>]) != 385. Failed Output: 0 **************** Problem 8 tests ************** FAIL because f(50) != 12586269025. Failed Output: 0 0 MAC:Thu Mar 03:13:14:loopy> !vi vi loopy.py >>> for k, item in enumerate("cows"): ... print(f"{3*k + 1} {item}") ... 1 c 4 o 7 w 10 s >>> x = [4,7,1,2,1] >>> x = reversed(sorted(x)) >>> x >>> list(x) [7, 4, 2, 1, 1] >>> x.sort(reverse=True) Traceback (most recent call last): File "", line 1, in AttributeError: 'list_reverseiterator' object has no attribute 'sort' >>> x = [4,7,1,2,1] >>> x.sort(reverse=True) >>> x [7, 4, 2, 1, 1] >>> x.sort() >>> x [1, 1, 2, 4, 7] >>> x.sort(reverse=False) >>> x [1, 1, 2, 4, 7] >>> x ="amanaplanacanalpanama" >>> x.find("a") 0 >>> x.rfind("l") 14 >>> x 'amanaplanacanalpanama' >>>