Basic python algorithms and modifiers, and python algorithm Decoration

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.