Python Foundation II

Source: Internet
Author: User

First, the dictionary type

*) dictionary is the only type of mapping in Python, key-value (hash table), the Dictionary object is mutable, but key must be used with immutable objects.

*) dictionary creation and dictionary worthy of access

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9B/A9/wKioL1llwSjj7WI4AAB0-Eq1-Oo875.png "title=" Screenshot from 2017-07-12 08-50-40.png "alt=" Wkiol1llwsjj7wi4aab0-eq1-oo875.png "/>

# #字典的内容在查看时不能通过索引来进行查看

*) Built-in method: Fromkeys

The key in the dictionary has the same value, the default is None

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9B/AA/wKiom1llwcqCzz9pAAAx6wT3QW8246.png "style=" float : none; "title=" screenshot from 2017-07-12 08-55-32.png "alt=" Wkiom1llwcqczz9paaax6wt3qw8246.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9B/A9/wKioL1llwcvxsryxAAAuxcRwfaI586.png "style=" float : none; "title=" screenshot from 2017-07-12 08-55-39.png "alt=" Wkiol1llwcvxsryxaaauxcrwfai586.png "/>


*) The dictionary iterates through the access

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9B/AA/wKiom1llwhDB8uVfAAA2cgepp3M431.png "title=" Screenshot from 2017-07-12 08-56-44.png "alt=" Wkiom1llwhdb8uvfaaa2cgepp3m431.png "/>

*) Addition of Key-value in dictionary

Dic[key] = value

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9B/AA/wKiom1llwkWiRvkXAABFDHohvk0174.png "title=" Screenshot from 2017-07-12 08-57-28.png "alt=" Wkiom1llwkwirvkxaabfdhohvk0174.png "/>

# #通过字典的添加发现, dictionaries are unordered data types

*) Dictionary deletion

* *) Delete the elements of the dictionary based on the key value

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/A9/wKioL1llwqHifkDKAABLZhyTM64432.png "title=" Screenshot from 2017-07-12 08-58-23.png "alt=" Wkiol1llwqhifkdkaablzhytm64432.png "/>

* *) random deletion of dictionary elements, return (Key,value)

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/AA/wKiom1llws2xemhxAABKUQmfUnU152.png "title=" Screenshot from 2017-07-12 08-58-44.png "alt=" Wkiom1llws2xemhxaabkuqmfunu152.png "/>

* *) Delete all elements in the dictionary

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9B/AA/wKiom1llwu3i2lzMAAA10ZuyqyQ498.png "title=" Screenshot from 2017-07-12 08-59-02.png "alt=" Wkiom1llwu3i2lzmaaa10zuyqyq498.png "/>

* *) Delete the dictionary itself

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9B/A9/wKioL1llwwjg7EBKAABX4ziwHas089.png "title=" Screenshot from 2017-07-12 08-59-08.png "alt=" Wkiol1llwwjg7ebkaabx4ziwhas089.png "/>

*) Common methods of dictionaries

* *) Dic.get ()

If key exists in the dictionary, the corresponding value value is returned

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9B/A9/wKioL1llw1DjnGTvAAAwk_SUQb4202.png "title=" Screenshot from 2017-07-12 09-01-12.png "alt=" Wkiol1llw1djngtvaaawk_suqb4202.png "/>

* *) Dic.keys ()

Returns all key values in the dictionary

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9B/AA/wKiom1llw3Tw2pcvAAAxwwZ3w4U196.png "title=" Screenshot from 2017-07-12 09-01-58.png "alt=" Wkiom1llw3tw2pcvaaaxwwz3w4u196.png "/>

* *) Update of dictionary content

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9B/A9/wKioL1llw5jSi1u7AABYBCUzDf0362.png "title=" Screenshot from 2017-07-12 09-04-05.png "alt=" Wkiol1llw5jsi1u7aabybcuzdf0362.png "/>

* *) Check if a key value exists in the dictionary

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9B/AA/wKiom1llw7uDVziyAABKywjkmOk684.png "title=" Screenshot from 2017-07-12 09-05-24.png "alt=" Wkiom1llw7udvziyaabkywjkmok684.png "/>

Second, function

*) define Functions

* *) def keyword, which in turn writes out the function name, parentheses, arguments in parentheses, and colons:

* *) write function body in indent block, return value of function returned with return statement.

def fun ():

Pass

Return

*) Call function

Python has built in a lot of useful functions that we can call directly:

* *) sum () # #求和内置函数

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9B/AA/wKioL1llxemw0qIaAAAgiO0QV6I840.png "title=" Screenshot from 2017-07-12 09-07-32.png "alt=" Wkiol1llxemw0qiaaaagio0qv6i840.png "/>

* *) ABS () # #求绝对值

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/AA/wKioL1llxg2AWfdEAAAT3-Y8vMw312.png "title=" Screenshot from 2017-07-12 09-11-09.png "alt=" Wkiol1llxg2awfdeaaat3-y8vmw312.png "/>

* *) CMP () # #比较函数

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/9B/AA/wKioL1llxjajcL-AAAAlgOkamHA242.png "title=" Screenshot from 2017-07-12 09-12-24.png "alt=" Wkiol1llxjajcl-aaaalgokamha242.png "/>

*) function type conversion

int (obj) # #将字符串转为整型

float (obj) # #转化为浮点型

STR (obj) # #转化为字符串型 *) What is the name of a function

* *) The function name is similar to the variable name, which is actually a reference to a function object;

* *) gives the function an "alias": the function name is assigned to a variable

*) NULL function

Define an empty function that does nothing and can use pass statements;

Pass can be used as a placeholder, and do not think how to write the code of the function, you can put a pass, the entire code first run up

def fun ()

Pass


*) parameter Check

* *) When the function is called, if the number of arguments is incorrect, the Python interpreter will automatically check out and exceed TypeError;

* *) If the parameter type is incorrect, the Python interpreter will not be able to check it for us.

* *) data type checking can be implemented with built-in function isinstance

*) function return value

* *) function return value with return keyword;

* *) returns a tuple that can omit parentheses;

* *) The Python function returns a multivalued value in fact it returns a tuple

* *) When the function is complete without a return statement, auto return None

*) function parameters

* *) when there are multiple parameters, change the large put in front, small changes in the back;

The required parameter is before the default parameter is

*) Variable parameters

Variable parameter is the number of parameters passed in is variable, can be 1, 2 to any, or can be 0

*args

*) keyword Parameters

* *) keyword parameter allows to pass in 0 or any parameter with parameter name;

* *) These keyword parameters are automatically assembled into a dict within the function;

* *) keyword parameter **kwargs

Iii. Advanced features of functions

*) Iteration

This list or tuple can be traversed through a for loop, which we call iteration (iteration)

As long as it is an iterative object, whether there is subscript or not, can iterate, for example, Dict can iterate

*) How to tell if an object can iterate

Judging by the iterable type of the collection module

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/AB/wKioL1lly2yQnZaeAAANnfS8A68549.png "style=" float : none; "title=" screenshot from 2017-07-12 09-21-36.png "alt=" Wkiol1lly2yqnzaeaaannfs8a68549.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9B/AB/wKioL1lly2zSLdVSAAAZenz-BjA313.png "style=" float : none; "title=" screenshot from 2017-07-12 09-22-41.png "alt=" Wkiol1lly2zsldvsaaazenz-bja313.png "/>

*) using Python's built-in enumeration method, enumerate a list into an index-element pair

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9B/AB/wKioL1lly8mTHKPbAAAhYtbiKDU558.png "title=" Screenshot from 2017-07-12 09-23-37.png "alt=" Wkiol1lly8mthkpbaaahytbikdu558.png "/>

*) for loop, referencing two variables at the same time, and iterating over the display

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9B/AB/wKiom1llzBWCWWhBAAAppUzbvL8934.png "title=" Screenshot from 2017-07-12 09-24-42.png "alt=" Wkiom1llzbwcwwhbaaappuzbvl8934.png "/>

*) List-generated

Range Module

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/AB/wKiom1llzF7QbCAaAAAdgjjRy04740.png "title=" Screenshot from 2017-07-12 09-25-44.png "alt=" Wkiom1llzf7qbcaaaaadgjjry04740.png "/>

List-generation:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9B/AB/wKioL1llzIOCJEHyAAAjsXcYMd0441.png "title=" Screenshot from 2017-07-12 09-27-11.png "alt=" Wkiol1llziocjehyaaajsxcymd0441.png "/>

# #循环太麻烦, and list generation can use a single line of statements instead of loops to generate the above list

*) List Builder

* *) Why List Builder is required:

Through the list generation, we can directly create a list, subject to memory constraints, the list capacity is certainly limited;

Create a list of 1 million elements that occupy a large amount of storage space;

* *) What the generator is:
The subsequent elements are continually extrapolated during the loop, so that you do not have to create a complete list, which saves a lot of space. In Python, this side loop is calculated as a mechanism, called the generator

# #使用g. Next () method reads the element sequentially (troublesome)

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9B/AB/wKioL1llzcWBi3HUAABpse00OK0513.png "title=" Screenshot from 2017-07-12 09-28-30.png "alt=" Wkiol1llzcwbi3huaabpse00ok0513.png "/>

# #使用for循环 (Quick and easy)

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9B/AB/wKioL1llzePCGipFAAATK3XnBVk802.png "title=" Screenshot from 2017-07-12 09-29-09.png "alt=" Wkiol1llzepcgipfaaatk3xnbvk802.png "/>

Python Foundation II

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.