Python Basic Learning---2

Source: Internet
Author: User

1) Condition judgment and circulation

If ....... else: .....
For xx in yy: ... while xx: ...

2) Dict and set:

Dict

{Key:val} using Key-value storage, with extremely fast lookup speed, key is immutable

Compared with list, Dict has the following characteristics: The search and insertion speed is very fast, will not increase with the increase of key, need to occupy a lot of memory, memory waste much


Set

A combination of key.  ([Key,key,key]) [] does not mean a list, repeating elements are filtered


3) function

def definition

Parameters of the function:

Default parameter: A default value is given when the function is defined. Note: The default parameter must be followed, and the default parameter must point to the immutable object, otherwise you will change the value of the default parameter as soon as you call it

Variable parameter: *numbers use numbers when calling parameters below. But inside the function, this numbers is a tuple. Not a number

Keyword parameters: **kw kw accepts a dict, which is the legendary dictionary. Owning key and value

def person (name,age,**kw): print ' name: ' Name, ' Age: ' Age, ' other: ' kw

Parameter combination: Order is important required parameter, default parameter, variable parameter, keyword parameter

Recursive functions:

There is an if to terminate the recursion. Otherwise, it keeps calling itself in else.


4) Advanced Features

Slices: List and tuple a[1:3] intercept starting from 1 to 3 end not including 3. equals = = >[1,3)

Iteration: For Loop

For key in Value:print key


List Builder: [] expression or an expression of an argument in an equation, or a source.

Import OS [D for D in Os.listdir ('. ')]

Generator function:

Use ()

In the process of the cycle of continuous calculation of subsequent elements, saving space

If a function definition contains the yield keyword, then the function is no longer a normal function, but a generator

The generator is not the same as the function in which the function is executed sequentially. Encountered return returned. Next time in this way. The generator is not the same, and each call to Next () is performed. When yield is met, it is returned, and the next time it is executed again from this yield place.

This article from "ehealth" blog, declined reprint!

Python Basic Learning---2

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.