The fifth day of python learning and the fifth day of python Learning

Source: Internet
Author: User

The fifth day of python learning and the fifth day of python Learning

1. decorator:

#! /Usr/bin/env python3

User_status = False # change this value to True when the user logs on.

Def login (auth_type): # upload the module to be executed from here

Def auth (func ):

Def inner (* args, ** kwargs): # define another function.

If auth_type = "qq ":

_ Username = "tom" # pretend that this is the user information stored in the DB

_ Password = "abc123" # pretend that this is the user information stored in the 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) # Check here. If the verification is passed, call the corresponding function.

Else:

Print ("only support qq ")

Return inner # When you call login, only the inner's memory address is returned. The inner function will be executed only when you add () to the next call.

Return auth

Def home ():

Print ("--- homepage ----")

@ Login ('qq ')

Def python ():

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

Def SQL ():

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

@ Login ('weibo ')

Def css ():

Print ("---- css area ----")

Home ()

Python ()

Running result:

--- Homepage ----
User: tom
Pasword: abc123
Welcome login ....
---- Python zone ----

Note: reference learning address: http://www.cnblogs.com/alex3714/articles/5765046.html

2. def w1 ():

Def B (c, d ):

Print (111)

Return B

@ W1

Def show ():

Print ("show ")

Note: Run w1 first, and use the function name of the function you decorated as a parameter, that is, w1 (show), the show function is redefined, and w1 (show) returns a new show = B.

Note: @ w1 () contains parameters. Run w1 () first, and then follow the steps above for subsequent operations.

3. recursive algorithms are a process of directly or indirectly calling their own algorithms. In computer programming, recursive algorithms are very effective in solving a large variety of problems. They often make the description of algorithms concise and easy to understand.

Features of recursive algorithms:

(1) recursion is to call itself in a process or function.

(2) When using a recursive policy, there must be a clear recursive termination condition called the recursive exit.

(3) recursive algorithms are usually simple in solving problems, but they are less efficient in solving problems. Therefore, designing programs using recursive algorithms is generally not recommended.

(4) During the recursive call process, the system opens a stack for storing the return points and local volumes of each layer. Excessive recursion times may cause stack overflow.

There are generally three requirements for recurrence in recursive algorithms:

First, each call is scaled down (usually halved );

Second, there is a close relationship between two adjacent duplicates. The previous one should be prepared for the next one (the previous output is usually used as the next input );

Third, when the scale of the question is extremely small, you must provide a direct answer instead of making a recursive call, because each recursive call is conditional (the scale is not the size of the direct answer ), unconditional recursive call will become an endless loop and cannot end normally.

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)

Running result: 0 1 (the sum of the first two numbers equals to the next number and the Fibonacci series .)
1
2
3
5
8
13
21
34

 

5. def calc (n ):

Print (n)

If n/2> 1:

Res = calc (n/2)

Return res

Calc (1, 100)

Running 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, 1,600 ))

Print (data)

Binary_search (data, 400)

Running 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. algorithms:

Matrix Rotation 90 degrees

#! /Usr/bin/env python3

Data = [[col for col in range (10)] 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 is used to traverse the elements in the sequence and Their subscript.

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)

Running 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]

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

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

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

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

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

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

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

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

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

 

Related Article

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.