Python basic Learning Code functions and functional programming

Source: Internet
Author: User

DEF&NBSP;FUNC1 ():    print  ' Hello world ' res = func1 () Print type ( RES) DEF&NBSP;FUNC2 ():     return [' xyz ', 10000,-98]ATUPLE&NBSP;=&NBSP;FUNC2 () x, Y, Z &NBSP;=&NBSP;FUNC2 () print x,y,zdef func3 ():    return  ' xyz ', 1000,-98x,y,z  = func3 () Print x,y,zdef func4 ():     return [' xyz ', 1000,-98, ' xxx ' ]x,y,z,d = func4 () Alist = x,y,z,dprint alisttrue = lambda :trueprint  true () sum = lambda x,y:x + ysumm = lambda x,y=4:x +  Yatuplet = lambda *zaz:zazprint atuplet (' A ', 1) adictt = lambda **z:zprint &NBSP;ADICTT (x=3,y=5) from random import randintdef functest (ARG):     Return arg % 2allnums = []for eachnum in range (9):     allnums.append (Eachnum) Print filter (Functest,allnums) Allnums = []for eachnum in range (9):#     print eachnum    ra = randint (1,99) #     Print ra    allnums.append (RA) #print  filter (lambda x:x%2,allnums) #print  [i for i in allnums if i%2]print [n for n in [ Randint (1,99)  for i in range (9)] if n%2]print map (lambda x:x+2,[i  for i in range (9)]) Print map (Lambda x:x**2,[int (i)  for i in  range (9)]) Print map (Str,[i for i in range (9)) Print map (lambda x,y:x+ y,[1,2,3],[1,2,3]) Print map (lambda x,y: (x+y,x-y), [1,2,3],[1,2,3]) Print map (None,[1,2,3],[1,2,3] ) Print reduce (Lambda x,y:x+y,[i for i in range (3)]) from operator  import  mul,addfrom Functools import partialadd1 = partial (add,1) mul100 = partial (mul,100) basetwo  = partial (int,base=2) basetwo.__doc__ =  ' convert base 2 string to  an int ' Print basetwo (' 10010 ') import tkinterroot = tkinter.tk () mybutton =  partial (tkinter.button,root,fg= ' white ', bg= ' Blue ') B1 = mybutton (text= ' button1 ') b2 =  MyButton (text= ' button2 ') Qb = mybutton (text= ' quit ', bg= ' red ', Command=root.quit) B1.pack () B2.pack () Qb.pack (fill=tkinter.x,expand=true) root.title (' pfas! ') Root.mainloop () is_this_global =  ' xyz ' Def foo ():     global is_this_ global    this_is_local =  ' abc '     is_this_global =   ' Def '     print this_is_local + is_this_globaldef foor ():     m = 3    def bar (): &NBSP;&NBSP;&NBSP;&Nbsp;    n = 4        print m +  n    print m    bar () def counter (start=0):   &NBSP;&NBSP;COUNT&NBSP;=&NBSP;[START]&NBSP;&NBSP;&NBSP;&NBSP;DEF&NBSP;INCR ():      &NBSP;&NBSP;&NBSP;COUNT[0]&NBSP;+=&NBSP;1&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;COUNT[0]     return incrcount = counter () output =  ' <int %r id =% #0x  val=%d> ' w = x = y = z = 1def f1 ():   &NBSP;&NBSP;X&NBSP;=&NBSP;Y&NBSP;=&NBSP;Z&NBSP;=&NBSP;2DEF&NBSP;F2 ():     y = z &NBSP;=&NBSP;3&NBSP;&NBSP;&NBSP;&NBSP;DEF&NBSP;F3 ():         z =  4        print output% (' W ', ID (w), W)          p rint output% (' x ', ID (x), X)         print output% (' Y ', id (y), y )         print output% (' Z ', id (z), z)     clo  = f3.func_closure    if clo:         print  ' f3 closure vars: ', [Str (c)  for c in clo]     else:        print  ' No f3 closure vars ' &NBSP;&NBSP;&NBSP;&NBSP;F3 ()     clo = f2.func_closure    if  clo:        print  ' f2 closure vars: ', [Str (c)  for c in clo]    else:         print  ' No f2 closure vars '     f2 ()     clo  = f1.func_closure    if clo:        print  ' F1 closure vars: ' , [Str (c)  for c in clo]    else:         print  ' No f1 closure vars ' from time import timedef logged ( When):     def log (F,*args,**kargs):         print  "called:function:%sargs:%skargs:%s"% (F,args,kargs)     def pre_logged (f ):         def wrapper (*args,**kargs):             log (F,*args,**kargs)              return f (*args,**kargs)          Return wrapper    def post_logged (f):         def wrapper (*Args,**kargs):             now = time ()             try:                 return f (*args,**kargs)              finally:                 log (F,*args,**kargs)                  print  ' time delta:%s '  %  (Time ( )-now)         return wrapper    try:         return {' pre ':p re_logged, ' Post ':p ost_logged}[when]     except keyerror,e:        raise valueerror (e), " must be  ' pre ' &Nbsp;or  ' Post ' @logged (' post ') Def hello (name):    print  ' Hello, ', Namehello (' world! ') X = 10def ffoo ():     y = 5    bar =  lambda z:x+z    print bar (y) j,k = 1,2def proc1 ():     j,k = 3,4    print  ' j==%d and k==%d '    %  (j,k) def proc2 ():     j = 6    proc1 ()     print  ' j==%d and k==%d '  %  (j,k) K = 7proc1 () print   ' j==%d and k==%d '  %  (j,k) j = 8proc2 () print  ' j==%d and k==%d '  %  (j,k) def max2 (ARG1,ARG2):    if arg1 > arg2:         return arg1    elif arg1 ==  Arg2:        return  ' equal '     else:         return arg2max22 = lambda a,b:a if a > b else  bmin22 = lambda a,b:a if a < b else bdef heji (A, B):     return a+b,a*bx,y = heji (3,4) def mymin (a,b,*num):     minnum = min22 (b)     for each in num:         minnum = min22 (Minnum,each)     return  Minnumdef mymax (A,b,*num):     maxnum = max22 (A, b)      for each in num:        maxnum = max22 (Maxnum , each)     return maxnumtrantime = lambda m: (Unicode (M&NBSP;/&NBSP;60), Unicode (M&NBSP;%&NBSP;60)print  ': '. Join (trantime) a = [' Jia ', ' wo ', ' ma ']b = [' Get ', ' Hoa ', '? '] Print map (none,a,b) Print zip (A, B) def oddyear (y):    if  (y %  4 == 0 and y % 100 != 0)  or y % 400 == 0 :         return yprint filter (Oddyear,range (1999,2030)) print  [y for y in range (1999,2030)  if  (Y % 4 == 0 and  y % 100 != 0)  or y % 400 == 0]print reduce (lambda  x,y:x+y,range (6))  / float (6) Cl = lambda x:x.strip () Res = map (Cl,open ( ' E:\\thefile.txt ') Import timedef timeit (ARG):    starttime =  Time.clock ()     result = arg    endtime =  Time.clock ()     return  (Result,endtime-starttime) Def arg (A, b):     return a * bprint  timeit (Arg (3,4)) Mult = lambda x,y:x * yprint reduce (Mult,range (9) [1:])


This article is from the "XWB" blog, make sure to keep this source http://xiewb.blog.51cto.com/11091636/1792275

Python basic Learning Code functions and functional programming

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.