一 、函数定义及使用
def calculate(x, y):
if y == 0:
return None
else:
return x + y, x - y, x * y, round(x / y, 2)
x, y = 4,6
print(calculate(x, y))
二、Lambda函数
numbers = [1, 3, 5, 7, 9, 11]
n = 7
greater_than_n = list(filter(lambda x: x > n, numbers))
print(greater_than_n)
三、递归函数
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
n = 5
print(factorial(n))