The fifth Python talk

Source: Internet
Author: User

First, the bubbling Algorithm 1, the value of two variables are interchanged
A1 = 123a2 = 456# to swap the values of A1 with A2 requires an intermediate variable (temp) temp = a1# To assign the value of A1 to Temp (temp=123) a1 = A2  #将a2的值赋值给a1, at which time the A1=456A2 = temp# assigns the value of temp to A2, at which time A2=123print (A1) print (A2)

Results:

A1 = 456A2 = 123

2. Bubble algorithm
Li = [33,2,10,1]for j in Range (1,len (LI)): For    I in range (Len (LI)-j):    # i = 0   1   2    3    # Li[0]   1   2    3    # Li[1]   2   3    4   length equals 3 here to 4 is out of range so Len (LI) to subtract 1        if li[i] > li[i + 1]:            temp = li[i]            li[i] = li[i + 1]            li[i + 1] = Tempprint (LI)

Note: The For loop of the inner layer is to find out the maximum value in the list, and the outer for loop is to sort the values other than the maximum, and finally the order of the list becomes small to large.

Second, recursive algorithm
    • Other functions can be called inside the function, and if a function calls itself within it, the function is a recursive function.
    • When we use recursive functions, we must have a definite recursive end condition (recursive exit).

Recursive algorithms are generally used to solve three kinds of problems

    • The definition of data is recursively defined (Fibonacci function)
    • The solution to the problem is to follow the recursive algorithm
    • The structural form of the data is defined recursively

The disadvantage of recursion: the problem of recursive algorithm is less efficient, in the process of recursive call, the system for each layer of the return point, the local amount, etc. to open up a stack to store, recursive too many times prone to overflow stack

Here, the Fibonacci sequence is used to demonstrate recursion:

Fibonacci Sequence:

0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765

#0, 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987li = []def F1 (DEPTH,A1,A2): #为了更好的观察程序的每一步斐波那契数列的动向, We define a depth to observe the # Fibonacci sequence starting from 0 li.append (A1) #将每一次循环的a1放到li列表中这就是要找的斐波那契数列    Print ("This is the first%s cycle:"%DEPTH,A1,A2) # Number of cycles printed, a1, a2if depth = = 5: #当斐波那契数列的深度到5的时候将li列表返回给调用者        return li    a3 = A1 + A2    r = F1 (Depth+1, A2, A3) #用变量r来接收 The value of the F1 function    return Rret = F1 (1,0,1) #执行函数f1, which is also the result of the function's entrance print (ret) print function execution

Results:

This is the 1th cycle: 0 1 This is the 2nd cycle: 1 1 This is the 3rd round: 1 2 This is the 4th cycle: 2 3 This is the 5th cycle: 3 5[0, 1, 1, 2, 3]

Here we look at the dynamic presentation in the recursive execution process:

Execution Flow diagram:

Three, the decoration device

Adorners are actually functions.

The syntax of the adorner:

The adorner begins with @ , followed by the name of the adorner function and optional parameters , followed by an optional parameter that the adorner declares to be a modified function and a decorative function .

Demo:

@outer

1, execute outer function, pass index as parameter

2, the return value of the outer, re-assigned to the index

def outer (func):    def Inner (a1,a2):        print ("123")        ret = func (A1,A2)        print ("456")        return ret    return Inner@outerdef Index (A1,A2):    print ("laborious")    

The execution flow of this adorner:

1, the execution process code of the adorner executes from top to bottom 2, the DEF outer (func) function reads the whole into memory, does not do any processing 3, encounters @outer    * executes outer function, passes index as parameter    

Dynamic display of the adorner's execution process:

Multiple adorners
def outer_0 (func):    def Inner (*arg,**kwargs):        print ("3.5")        ret = func (*arg,**kwargs)        print ("789")        return ret    return innerdef outer (func):    def Inner (*arg,**kwargs):        print ("123")        ret = func (*arg, **kwargs)        print ("456")        return ret    return inner@outer_0@outerdef index (A1,A2):    print (" Dead End-de Marcia ")    return a1 + a2m = Index print (m)

Execution process:

Two adorners decorate the same function adorner: * Perform decorative functions, pass the decorated function as a parameter * re-copy the return value of the adorner function to the decorated function 1. Put Def outer_0 (): In Memory 2. Set DEF outer (): Put it in memory. 3. Encountered the first adorner outer_04. Since this is a two adorner together, the program will continue to look for the next adorner outer* execute the outer function * to read the inner function inside the outer function into memory, Since there is no call to function in inner, this function does nothing * executes the OUTER_0 function * reads the OUTER_0 function into memory, continues to read the inner function inside the outer_0, and there is no action in the inner inside the function * Inner The return value of the function to the index function (this is equivalent to renaming the inner function inside the function to the index function) Outer_0 give it to him first, perform its internal inner* print 3.5* execute ret = func () Here the Func function is equivalent to the inner layer function of the outer function inner* print 123* execute outer function in RET = func () * to @outer_0 Adorner * print "dead knock--de Marcia" * Print outer function 456* Print 789 return value ret back to caller m

Dynamic presentation of the execution process:

The fifth Python talk

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.