1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| def my_abs(x): if x >= 0: return x else: return -x
from test import my_abs
def nothing(): pass
def my_abs(x): if not isinstance(x, (int, float)): raise TypeError('bad operand type')
import math def move(x, y, step, angle=0): nx = x + step * math.cos(angle) ny = y - step * math.sin(angle) return nx, ny x, y = move(...) r = move(...)
def add_end(L=[]): L.append('END') return L
def add_end(L=None): if L is None: L = [] L.append('END') return L
def calc(numbers): sum = 0 for n in numbers: sum = sum + n * n return sum calc([1, 2, 3])
def calc(*numbers): sum = 0 for n in numbers: sum = sum + n * n return sum calc(1, 2, 3) nums = [1, 2, 3] calc(*nums)
def person(name, age, **kw): print('name:', name, 'age:', age, 'other:', kw) person('Michael', 30) person('Bob', 35, city='Beijing') person('Adam', 45, gender='M', job='Engineer') extra = {'city': 'Beijing', 'job': 'Engineer'} person('Jack', 24, **extra)
def person(name, age, *, city, job): print(name, age, city, job) person('Jack', 24, city='Beijing', job='Engineer')
def f1(a, b, c=0, *args, **kw): print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw) f1(1, 2, 3, 4, 'a', d = 1) def f2(a, b, c=0, *, d, **kw): print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw) f2(1, 2, d=99, ext=None) args = (1, 2, 3, 4) f1(*args, **kw) args = (1, 2, 3) kw = {'d': 99, 'x': '#'} f2(*args, **kw)
def fact(n): if n == 1: return 1 return n * fact(n - 1) fact(5)
def fact(n, temp): if n == 1: return temp return fact(n - 1, n * temp)
|