python函数入门

递归方式实现阶乘

1
2
3
4
5
6
def factorial(num) -> int:
if num > 1:
return num * factorial(num - 1)
else:
return num
print(factorial(3))

python函数入门例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def sum(*args) -> int:
'''
文档字符串
sum函数,可以输入不定长度的数字参数,返回int类型
'''
print(args)
total = 0
for i in args:
total += i
print( locals())
return total

ls=(1,2,3,4,5)
sum(*ls)