(进度:1/3)
内容链接:
https://tianchi.aliyun.com/specials/promotion/aicamppython?spm=5176.19782939.J_5614344200.2.3ad4564b61mfN0
- 这次任务的内容在之前学的时候就感觉学的不是很好,这次主要是复健+补充学习。
本次任务分三天学习
二、学习内容: 1. 函数- 可变参数:即在形参的前面加上*号,能够接受任意多个参数,将传入的参数自动存放入元组
- 关键词参数:即在形参前面加上**号,即能接受多个参数(接收类似于a=1这种样子,关键词参数会自动将传入的这种参数变成字典的样子)
- 命名关键词参数:这是对于出阿鲁参数的名有限制,具体看代码来理解
def student(name,age,*,sports): print(name,age,sports) #这里的sports前面加上了*号,也就是命名关键词参数了,只接受sports关键词名传入的参数, #若是传入像score=98这种就会发生报错,因为我对关键词名进行了限制
- 参数组合的时候注意放置的位置,如(位置参数,默认参数,可变参数,关键词参数)或者(位置参数,默认参数,命名关键词参数,关键词参数)
- nonlocal:用在外部嵌套了函数,并且要使用外部函数的变量上。
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
- filter:一般是用于过滤,过滤掉不符合条件的参数
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]
- 与filter相反的是map函数,能够根据提供的函数对指定序列操作。
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 第一天学习(水课上看的,效率不咋高)
四、学习产出:- 第一天:感觉补充了点知识



