函数参数
def showInfo(name, age, addr, money):
age += 1
print(f'姓名: {name}, 年龄: {age} , 地址: {addr}, 余额: {money}')
showInfo("Bob", 18, "铁岭的", 888)
showInfo(money=9.9, addr='北京的', name='Eric', age=19)
showInfo("李四", 23, "上海的", money=22)
def showInfo2(name, addr, age=0, money=0):
print(f'姓名: {name}, 年龄: {age} , 地址: {addr}, 余额: {money}')
showInfo2("刘能", "象牙山")
showInfo2("赵四", "象牙山", 40, 9999)
showInfo2("赵四", "象牙山", 40)
showInfo2("赵四", "象牙山", age=30, money=8888)
showInfo2(money=9.9, addr='北京的', name='HAHA', age=19)
递归函数
def f(n):
if n == 1:
return 1
return f(n-1) + n
print(f(100))
print(f(5))
def jieche(n):
if n == 1:
return 1
return jieche(n-1)*n
print(jieche(3))
print(jieche(5))
def fibo(n):
if n in [1, 2]:
return 1
return fibo(n-1) + fibo(n-2)
print(fibo(3))
print(fibo(4))
print(fibo(5))
print(fibo(6))
```
```python
from random import randint
ls = [randint(1, 20)for _ in range(10)]
print(ls)
print(sum(ls))
def arr_sum(ls):
mySum = 0
for i in ls:
mySum += i
return mySum
print(arr_sum(ls))
def qiuhe(ls):
if len(ls) ==0:
return 0
if len(ls) == 1:
return ls[0]
return ls[0] + qiuhe(ls[1:])
print(qiuhe([]))
print(qiuhe(ls))
```
```python
sums = lambda x, y: x + y
print(sums(12, 13))
def cal(func, x, y):
"""
:param func: 要求是一个函数
:param x: 整数值
:param y: 整数值
:return: 对应关系的结果
"""
return func(x, y)
def f1(x, y):
return x + y
def f2(x, y):
return x * y
print(cal(f1, 10, 20))
print(cal(f2, 10, 20))
print(cal(lambda x, y: x-y, 10, 20))
```