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
| def add(x, y): print "x is {0} and y is {1}".format(x, y) return x + y
add(5, 6)
add(y=6, x=5)
def varargs(*args): return args
varargs(1, 2, 3)
def keyword_args(**kwargs): return kwargs
keyword_args(big="foot", loch="ness")
def all_the_args(*args, **kwargs): print args print kwargs """ all_the_args(1, 2, a=3, b=4) prints: (1, 2) {"a": 3, "b": 4} """
args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} all_the_args(*args) all_the_args(**kwargs) all_the_args(*args, **kwargs)
def pass_all_the_args(*args, **kwargs): all_the_args(*args, **kwargs) print varargs(*args) print keyword_args(**kwargs)
x = 5
def set_x(num): x = num print x
def set_global_x(num): global x print x x = num print x
set_x(43) set_global_x(6)
def create_adder(x): def adder(y): return x + y return adder
add_10 = create_adder(10) add_10(3)
(lambda x: x > 2)(3) (lambda x, y: x ** 2 + y ** 2)(2, 1)
map(add_10, [1, 2, 3]) map(max, [1, 2, 3], [4, 2, 1])
filter(lambda x: x > 5, [3, 4, 5, 6, 7])
[add_10(i) for i in [1, 2, 3]] [x for x in [3, 4, 5, 6, 7] if x > 5]
|