2.1 python函数
2.1.1 函数自定义函数第一种方法第二种方法
2.1.2函数练习自定义求序列偶数个数的函数
2.1.1 函数
自定义函数
第一种方法
def function(x,y): return 'result’ 例:自定义一个函数,能求出一组给定数组中奇数的个数
def Sum(x
,y
=1):
return x
+y
第二种方法
y = lambda x:x+1
y
= lambda x
:x
**2
y1
= lambda x
:x
[1]
res
= Sum
(1, 2)
res
= y
(10)
res
= y1
(['hello',0])
print(res
)
2.1.2函数练习
自定义求序列偶数个数的函数
def su(x
):
z
= 0
for i
in x
:
if i
%2==0:
z
+= 1
return z
res
= su
([1,2,3,4,5,6])
print(res
)