Def func1 (): print ' Hello world ' res = func1 () print type (res) def FUNC2 (): return [' xyz ', 10000,-98]atuple = Func2 () x, y , z = 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 adictt (x=3,y=5) from random import Randintd EF Functest (ARG): return arg% 2allnums = []for eachnum in range (9): Allnums.append (eachnum) print filter (Functest,al lnums) allnums = []for eachnum in range (9): # print Eachnum ra = randint (1,99) # print RA allnums.append (RA) #prin T filter (lambda x:x%2,allnums) #print [i-I in allnums if I%2]print [n-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 (' 1 0010 ') 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 (): n = 4 print m + N print m Bar () def counter (start=0): Count = [start] def incr (): count[0] + = 1 return count[0] Return IncRcount = Counter () output = ' <int%r id=% #0x val=%d> ' W = x = y = z = 1def F1 (): x = y = z = 2def F2 (): y = z = 3 def f3 (): z = 4 Print output% (' W ', ID (w), W) print output% (' x ', ID (x), x) print output% (' Y ', I D (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 ' 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 WR Apper 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 WRA Pper Try:return {' Pre ':p re_logged, ' post ':p ost_logged}[when] except keyerror,e:raise valueerror (E), "M UST be ' pre ' 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 = = A Rg2: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 (A, A, 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/60), Unicode (m)) 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% = 0) or y% = = 0:return Yprint Filter (Oddyear,range (1999,2030)) print [Y for y in range (1999,2030) if (y% 4 = = 0 and y% = 0) or y% = = 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 Tim EIT (ARG): StartTime = time.clock () result = arg endtime = Time.clock () return (result,endtime-starttime) def ar G (A, B): Return a * bprint Timeit (ARG (3,4)) mult = Lambda x,y:x * yprint reduce (Mult,range (9) [1:])
The above is the Python Basic Learning Code function and functional programming content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!