Python note (3)-continue learning

Source: Internet
Author: User
Python notes

Recently, I have been busy reading English documents. By the way, I am familiar with English. I have to repeat it several times before I can take notes. You are reading beginning. Python. From. novice. to. Professional. You can download resources from the Internet.

 

Subject:

Why is there a way?

The answer is: laziness is a virtue.

Key words of method definition:

Def

Use callable to determine whether it is callable:

 
X = 1Y=Math. sqrtcallable (X)#FalseCallable (y)#True

Return values of methods:

DefHello (name ):Return 'Hello,'+ Name +'!'

There isAlgorithmImplementation: The sum of the first two numbers is the latter.

 
Fiber = [0, 1]ForIInRange (8): Fiber. append (fiber [-2] + fibs [-1]) Result: [0,1, 1, 2, 3, 5, 8, 13, 21, 34]

In this way, you can define a method implementation:

DefFibs (Num): Result= [0, 1]ForIInRange (num-2): Result. append (result [-2] + result [-1])ReturnResult

Execute the command in the exit method:

 
DefTest ():Print 'This is printed'ReturnPrint 'This is not'

If the method does not return anything, none is returned.

Method Parameters
The question of whether the parameters of the input method are changed is common in many languages.

Example:

 
DefTry_to_change (n): n='Mr. Gumby'Name='MRS. Entity'Try_to_change (name) Name#'Mrs. entity'

The parameters in the preceding example are not changed, so those parameters are variable.
String number tuples is an unchangeable type and cannot be changed. If we use a variable type as a parameter, we can implement the function that the parameter is changed in the method.

DefChange (n): N [0]='Mr. Gumby'Names= ['MRS. Entity','MRS. Thing'] Change (names) Names#['Mr. gumby', 'Mrs. action']

This part of content is actually similar to Java. I used to have a blog post: Click it to arrive.

You can use the following methods to confuse parameter meanings:

DefHello_1 (greeting, name ):Print '% S, % s!'%(Greeting, name) hello_1 (greeting='Hello', Name ='World')#Hello, world!

A method set for finding someone by name:

 Def Init (data ): #  1 Data [ ' First  ' ] = {} Data [  '  Middle  ' ] = {} Data [  '  Last  ' ] = {}  Def  Lookup (data, label, name ):  Return Data [label]. Get (name) # 2  Def  Store (data, full_name): names = Full_name.split () #  3      If Len (names) = 2: names. insert (1, '' ) #  4 Labels = '  First  ' , '  Middle  ' ,'  Last  '      For Label, name In Zip (labels, names ): #  5 People = Lookup (data, label, name)  If  People: people. append (full_name)  #  6      Else  : Data [label] [name] = [Full_name]

Usage:

Mynames ={} Init (mynames) store (mynames,'Magnus lie hetland') Lookup (mynames,'Middle','Lie')#['Magnus lie hetand']

Understanding:

1. The data structure of data is as follows: {'middle' :{}, 'last' :{}, 'first ':{}}
2. The get method finds the value based on the Value
3. You can add separators to the split method of string. The default Delimiter is space:

Test ='A, 2, D'Test. Split (',')#['A', '2', 'D']Name ='My XY dd'Names=Name. Split () Names#['My', 'xy', 'dd']

4. do not replace the values after insert.

 
Names. insert (1,'') Names#['My', '', 'xy', 'dd']

5. Example of zip method description:

 
X = [1, 2, 3] Y= [4, 5, 6] Zipped= Zip (x, y)#(1, 4), (2, 5), (3, 6)

6. If the name already exists in the label, add the full name.

* And ** in Parameters **

*: Indicates any number of parameters.

**: Indicates the dictionary parameter.

Example:

 
DefPrint_params_2 (title ,*Params ):PrintTitlePrintParamsprint_params_2 ('Params:', 1, 2, 3)

The result is

Params:
(1, 2, 3)

 
DefPrint_params (**Params ):PrintParamsprint_params (x= 1, y = 2, Z = 3)

Result: {'Z': 3, 'x': 1, 'y': 2}

 

You can use the * method to improve the storage Name:

 Def Store (data ,* Full_names ):  For Full_name In  Full_names: names = Full_name.split ()  If Len (names) = 2: names. insert (1,''  ) Labels = '  First  ' , '  Middle  ' , '  Last  '          For Label, name In  Zip (labels, names): People = Lookup (data, label, name) If  People: people. append (full_name)  Else  : Data [label] [name] = [Full_name]

Call:

D ={} Init (d) store (D,'Han Solo') Store (D,'Luke Skywalker','Anakin Skywalker') Lookup (D,'Last','Skywalker')#['Luke Skywalker ', 'anakin Skywalker']

 

Summary:

I can't use it at work, but it's good to take some time to learn and enrich myself.

Note can be used to read and learn. I hope that when I see it in the future, don't be discouraged, Don't be arrogant, 1.1 drops of learning may be useless in the future, however, patience may be required in this way.

Let's move on!

 

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

Hard work may fail, but not hard work will certainly fail.
Sharing

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.