Python notes (3) continue learning-Python tutorial

Source: Internet
Author: User
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:
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:

The code is as follows:


X = 1
Y = math. sqrt
Callable (x) # False
Callable (y) # True


Return values of methods:

The code is as follows:


Def hello (name ):
Return 'Hello, '+ name + '! '


There is an algorithm implementation: the sum of the first two numbers is the following number.

The code is as follows:


Fibs = [0, 1]
For I in range (8 ):
Fiber. append (fiber [-2] + fiber [-1])

Result: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]


In this way, you can define a method implementation:

The code is as follows:


Def fibs (num ):
Result = [0, 1]
For I in range (num-2 ):
Result. append (result [-2] + result [-1])
Return result


Execute the command in the exit method:

The code is as follows:


Def test ():
Print 'this is printed'
Return
Print '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:

The code is as follows:


Def try_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.

The code is as follows:


Def change (n ):
N [0] = 'Mr. gumby'

Names = ['Mrs. Entity ', 'Mrs. action']
Change (names)
Names # ['Mr. gumby', 'Mrs. list']


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:

The code is as follows:


Def hello_1 (greeting, name ):
Print '% s, % s! '% (Greeting, name)
Hello_1 (greeting = 'hello', name = 'world') # Hello, world!


A method set for finding someone by name:

The code is as follows:


Def init (data): #1
Data ['first'] = {}
Data ['ddle'] = {}
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:

The code is as follows:


MyNames = {}
Init (MyNames)
Store (MyNames, 'magn' Lie hetland ')
Lookup (MyNames, 'middle', 'Lil') # ['Magnus Lie hetland']


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:

The code is as follows:


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.

The code is as follows:


Names. insert (1 ,'')
Names # ['My, '', 'XY', 'DD']


5. example of zip method description:

The code is as follows:


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:

The code is as follows:


Def print_params_2 (title, * params ):
Print title
Print params
Print_params_2 ('params: ', 1, 2, 3)


The result is

Params:
(1, 2, 3)

The code is as follows:


Def print_params (** params ):
Print params
Print_params (x = 1, y = 2, z = 3)


Result: {'Z': 3, 'x': 1, 'y': 2}
You can use the * method to improve the storage name:

The code is as follows:


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:

The code is as follows:


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!

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.