
(进度:1/3)
内容链接:
https://tianchi.aliyun.com/specials/promotion/aicamppython?spm=5176.19782939.J_5614344200.2.3ad4564b61mfN0
本次任务分三天学习
二、学习内容: 1. 函数def student(name,age,*,sports): print(name,age,sports) #这里的sports前面加上了*号,也就是命名关键词参数了,只接受sports关键词名传入的参数, #若是传入像score=98这种就会发生报错,因为我对关键词名进行了限制
def nonlocal_test(): count=0 def tet2(): nonlocal count count+=1 return count return test2 val=nonlocal_test() print(val()) # 1
def make_counter(init):
counter = [init]
def inc(): counter[0] += 1
def dec(): counter[0] -= 1
def get(): return counter[0]
def reset(): counter[0] = init
return inc, dec, get, reset
inc, dec, get, reset = make_counter(0)
inc()
inc()
inc()
print(get()) # 3
dec()
print(get()) # 2
reset()
print(get()) # 0
def funX(x):
def funY(y):
return x * y
return funY
i = funX(8) 也就是i也变成了函数,需要传入向形参y传入参数
print(type(i)) #
print(i(5)) # 40
func=lambda *args: sum(args) print(func(1,2,3,4,5) #15
odd=lambda x: x%2==1 teplist=filter(odd,(1,2,3,4,5,6,7,8,9)) print(list(teplist)) #[1, 3, 5, 7, 9]
m1=map(lambda x: x**2,[1,2,3,4,5) #[1,4,9,16,25]
10.我们可以自己创造高阶函数:(但我感觉用的不是很多吧)
def apply_to_list(fun, some_list):
return fun(some_list)
lst = [1, 2, 3, 4, 5]
print(apply_to_list(sum, lst))
# 15
print(apply_to_list(len, lst))
# 5
print(apply_to_list(lambda x: sum(x) / len(x), lst))
# 3.0
2021年10月5日 20:11 至21:23 第一天学习(水课上看的,效率不咋高)
四、学习产出: