函数
def output():
print("666")
output()
666
def fun(name):
print(name + "5555")
fun("666")
6665555
def add(a, b):
return a + b
a = add(11, 12)
a
23
def add(a, b):
'a + b'
return a + b
add.__doc__
'a + b'
def fun(name, school):
print(name + "毕业于" + school)
fun("科大","我"), fun(school = "科大",name = "我")
科大毕业于我
我毕业于科大
def add2(*a):
print(a[0], a[1], a[2])
add2(1, 5, 9)
def add2(*a, b):
print(a[0], a[1], a[2], b)
add2(1, 5, 9, b = 999)
t = add2(1, 5, 9, b = 999)
print(t)
1 5 9 999
None
def fun2():
return [2, 3, 6]
a = fun2()
a
[2, 3, 6]
def fun2(b):
c = 50
print("c2:",c)
c = 100
fun2(100)
print("c3:", c)
c2: 50
c3: 100
def fun2(b):
global c
c = 50
print("c2:",c)
c = 100
fun2(100)
print("c3:", c)
c2: 50
c3: 50
def funx(x):
def funy(y):
return x * y
return funy
y = funx(40)
y(40)
1600
funx(40)(40)
1600
def funx(x):
t = 10
def funy(y):
nonlocal t
return x * t * y
return funy(10)
funx(40)
4000
f = lambda x : x * 2 + 6
f(4)
14
f = lambda x, y : x * 2 + 6 * y
f(2, 2)
16
filter(function or None, iterable)
过滤器 默认选择为真
def odd(x):
return x % 2
temp = range(10)
show = filter(odd, temp)
list(show)
[1, 3, 5, 7, 9]
list(filter(lambda x : x % 3, temp))
[1, 2, 4, 5, 7, 8]
list(map(lambda x : x * 3, range(5)))
[0, 3, 6, 9, 12]