Basic python algorithms and modifiers, and python algorithm Decoration
1. Bubble Sorting
For the size comparison of the Bubble Sorting implementation, the large index will move backward. In this cycle, the maximum value is directly moved to the end.
li = [125,56,78,23,]for i in range(len(li)-1): if li[i] > li[i+1]: temp = li[i] li[i] = li[i + 1] li[i + 1] = tempprint(li)[56, 78, 23, 125]
2. Recursion
The Fibonacci series is implemented based on Recursive methods, because the recurrence of wireless loops reports an error. Here we make a limit.
def f1(a1,a2): if a1 >1000: return print(a1) a3 = a1 + a2 f1(a2,a3)f1(0,1)01123581321345589144233377610987
How to recursively obtain the code implementation of the tenth digit in the final cut series
def f5(de,a1,a2): if de == 10: return a1 a3 = a1 +a2 r = f5(de + 1,a2,a3) return rprint(f5(1,0,1))34
3. decorator used to decorate a method, function, object, or class
Next we will add a simple decorator to a simple function.
def out(func): def inner(): print("hello") r = func() return r return inner@outdef f1(): print("f1")f1()hellof1
The @ symbol has a special meaning here. @ function name will execute this function and pass the function name below this line as a parameter to this function, and re-assign the value of out to f1.
Decorator writing with two parameters
def out(func): def inner(a,b): print("hello") r = func(a,b) return r return inner@outdef f1(a,b): print("f1") print(a + b)f1(1,2)hellof13
Decorator with multiple parameters in a function
def out(func): def inner(*arg,**kwargs): print("hello") r = func(*arg,**kwargs) return r return inner@outdef f1(a,b): print("f1") print(a + b)f1(1,2)hellof13
How to describe one function by using multiple decorator
def out(func): def inner(*arg,**kwargs): print("hello") r = func(*arg,**kwargs) return r return innerdef out1(func): def inner(*arg,**kwargs): print("heh") r = func(*arg,**kwargs) return r return inner@out@out1def f1(a,b): print("f1") print(a + b)f1(1,2)hellohehf13