1、冒泡处理
eg:
li = [11, 2, 3, 1, 444, 333, 22, 77, 88, 99, 66, 87] for k in range(1, len(li)-2): for i in range(len(li)-1): if li[i] > li[i+1]: temp = li[i] li[i] = li[i+1] li[i+1] = temp print(li)
结果输出:[1, 2, 3, 11, 22, 66, 77, 87, 88, 99, 333, 444]
2、递归
eg:利用递归获取斐波那契数列中的第 10 个数,并将该值返回给调用者
def func(n, a1, a2): if n == 10: return a1 a3 = a1 + a2 re = func(n+1, a2, a3) return re result = func(1, 0, 1) print(result)
》》》34
3、装饰器
def outer(func):
def inner(*arg, **kwargs):
print('A')
in_func = func(*arg, **kwargs)
print('B')
return in_func
return inner
@outer
def func1(a , b):
return a + b
@outer
def func2(a, b):
return a * b
result1 = func1(1, 3)
result2 = func2(2, 3)
print(result1," ",result2)
>>
A
B
A
B
4
6
@outer1
@outer2
def index() 一个函数可以有多个装饰器,先从上面的装饰器执行到下面