The fifth day of the Python learning Path

Source: Internet
Author: User

1. Decorator:

#!/usr/bin/env Python3

User_status = False #用户登录了就把这个改成True

def login (auth_type): #把要执行的模块从这里传进来

def auth (func):

def inner (*args,**kwargs): #再定义一层函数

if Auth_type = = "QQ":

_username = "Tom" #假装这是DB里存的用户信息

_password = "abc123" #假装这是DB里存的用户信息

Global User_status

if User_status = = False:

Username = input ("User:")

Password = input ("Pasword:")

If username = = _username and Password = = _password:

Print ("Welcome login ....")

User_status = True

Else

Print ("Wrong username or password!")

if User_status = = True:

return func (*args,**kwargs) # Look here, as long as the validation has passed, call the corresponding function

Else

Print ("Only support QQ")

Return inner #用户调用login时, only the memory address of the inner is returned, and the next time it is added () to execute the inner function

return auth

Def home ():

Print ("---home----")

@login (' QQ ')

def python ():

Print ("----python zone----")

Def SQL ():

Print ("----SQL zone----")

@login (' Weibo ')

def CSS ():

Print ("----CSS zone----")

Home ()

Python ()

Operation Result:

---home----
User:tom
Pasword:abc123
Welcome login ....
----Python area----

Note: Reference study address: http://www.cnblogs.com/alex3714/articles/5765046.html

2. Def W1 (a):

def b (c,d):

Print (111)

Return b

@w1

Def show ():

Print ("show")

Note: First execute the W1, the function name of the function that oneself decorates as parameter, namely W1 (show), show function redefine, W1 (show) return value new show= B.

Note: There are parameters in the @w1 () brackets, execute W1 () first, and then follow the steps as above.

3, recursive algorithm is a direct or indirect call to their own algorithm process. In the computer programming, the recursive algorithm is very effective to solve a large class of problems, it often makes the description of the algorithm concise and easy to understand.

The recursive algorithm solves the problem characteristic:

(1) Recursion is the invocation of itself in a procedure or function.

(2) When using a recursive strategy, there must be a definite recursive end condition called a recursive exit.

(3) The recursive algorithm usually appears very concise, but the recursive algorithm is inefficient in solving problems, so it is generally not advocated to design the program with recursive algorithm.

(4) in the process of recursive call system for each layer of the return point, local volume, such as open up a stack to store, too many recursive times can cause stack overflow and so on.

The recursive algorithm embodies the "repetition" generally has three requirements:

One is that each invocation shrinks in size (usually halved);

Second, there is a close relationship between two repetitions, the previous time to prepare for the next time (usually the previous output as the last input);

Thirdly, when the scale of the problem is very small, it is necessary to give the answer directly instead of recursive invocation, because each recursive invocation is conditional (the size does not reach a direct solution), the unconditional recursive call will become a dead loop and not the normal end.

4. def func (arg1,arg2,stop):

if arg1 = = 0:

Print (ARG1,ARG2)

Arg3 = arg1 + arg2

Print (ARG3)

If Arg3 < stop:

Func (Arg2,arg3,stop)

Func (0,1)

Run Result: 0 1 (the first two numbers add up to the next number, the Fibonacci sequence.) )
1
2
3
5
8
13
21st
34

5. Def calc (n):

Print (n)

If N/2 > 1:

res = Calc (N/2)

return res

Calc (100)

Operating result: 100 50.0 25.0 12.5 6.25 3.125 1.5625

6. Def binary_search (data_source,find_n):

mid = Int (len (data_source)/2)

If Len (data_source) >= 1:

If Data_source[mid] > find_n:

# data in left

Print ("Data in left of [%s]"% data_source[mid])

Binary_search (Data_source[:mid],find_n)

Elif Data_source[mid] < find_n: # data in right

Print ("Data in right of [%s]"% data_source[mid])

Binary_search (Data_source[mid:],find_n)

Else

Print ("Found find_s,", Data_source[mid])

Else

Print ("Cannot find.")

if __name__ = = ' __main__ ':

data = List (range (1,600))

Print (data)

Binary_search (data,400)

Run Result: Data in right of [300]
Data in left of [450]
Data in right of [375]
Data in left of [412]
Data in right of [393]
Data in left of [402]
Data in right of [397]
Data in right of [399]
Found find_s, 400

7. Algorithm:

Matrix Rotation 90 degrees

#!/usr/bin/env Python3

data = [[col for Col in Range]] for row in range (10)]

Print (data) for row in data:

Print (ROW)

Print ('-----------')

#r_index row subscript, c_index column subscript, tmp temporary storage, data[][] matrix address, such as data[0][0]=0.

For R_index,row in Enumerate (data): the #enumerate () function iterates over the elements in the sequence and their subscripts

For C_index in range (R_index,len (row)):

TMP = Data[c_index][r_index]

Data[c_index][r_index] = Row[c_index]

Data[r_index][c_index] = tmp

Print ('-----------')

For R in data:

Print (R)

Operating result: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6 , 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4 , 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- - ----------

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

[4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

[6, 6, 6, 6, 6, 6, 6, 6, 6, 6]

[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]

[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]

[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

The fifth day of the Python learning Path

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.