Python Learning Experience

Source: Internet
Author: User

One, advanced features:

1, slice: [Start:stop:step]

>>>l=range (6)>>>l[3,-1,2]resulte is [ 3]

2, Iteration

2.1 Press Itervalues ()

>>> d={1:2,2:3,3:4} for in D: ...      Print  1 22 33 4

2.2 Press Iteritems ()

 for inch D.iteritems ():      ... Print v ... (1, 2) (2, 3) (3, 4) returned as tuples

2.3 With index output: enumerate

 for  in Enumerate (['a','b','c' ]):...      Print I,value ... 0 A1 b2 C

2.4 Generator: The mechanism of the side loop computation, called the Generator (Generator), is useful for saving storage space, which is equivalent to the next in the list

def fib (max): ...     N,a,b=0,0,1      ...  while n<max: ...              yield b .....              b             =b,a+ ... n=n+1>>> fib (6) for in     fib (6): ... Print  112358

(ii), functional programming

1, higher order function: function name as parameter variable of method

def Add (x,y,abs):      ... return ABS (x) *abs (y)def  cut (     x,add): ... return x>>> cut (1, add) 0

2,map

The user input of the non-standard English name, into the first letter capitalization, other lowercase canonical name map (Lambda s:s.capitalize (), ['AC ','ac'][ac,ac]

Reduce

>>> L=map (Lambda s:s.capitalize (), ['ac','ac'  ])>>> reduce (lambda x,y:x+y,l)'AcAc'

Filter

>>>ImportMath>>>defIsprim (s): ... forIinchRange (2,int (math.sqrt (s) +1)):...             ifs%i==0: ...returnFalse ...returnTrue ...>>> Filter (Isprim,range (2,10))[2, 3, 5, 7]>>>Import Time>>>PrintTime.mktime (Time.localtime ())//Time Calculation1497605034.0

3,sorted x>y 1,x<y-1 x==y 0

Sorted ([1,3,2],Lambda x,y:y-x)//If y-x>0 is a two-digit exchange location, using bubbling sort [3, 2, 1]

4, Closure (Closure), using the Return function form:

def count ():    fs=[]     for in  Range (1,4)        def F (j):             def g ():                  return j*J            return  G        fs.append (f (i))    return FS  for inch count ():     Print I,j

5, anonymous function: Lambda

6, "Adorner" (Decorator): The method is enhanced, but there is no way to change

ImportFunctoolsdeflog (args):""""""Task="Pager"    defActual_log (func): @functools. Wraps (func)defWrapper (*args,**kw):Print '%s:%s ()'% (Task,func.__name__)//Two underlinereturnFunc (*args, * *kw)returnwrapperifHasattr (args,'__call__'):        returnactual_log (args)Else: Task=argsreturnActual_log@logdeff ():Pass@log ("Execute")defe ():Pass

5, Partial function

>>> max2=functool.partial (max,10)//10 aspart of the * args automatically added to the left >>> max2 (1,3,4,5)10
>>> def func (a,b,c):
>>> Print A,b,c
>>>func2=functools.partial (func,b=3) replacing key parameters
>>>func (1,c=4)
1 3 4

Python Learning Experience

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.