Python function Usage Rollup

Source: Internet
Author: User

# -*- coding: utf8 -*-#不带参数函数def  fun ():     print (' hello  World ') Fun ()   #hello  world#----------------------------------------#带参数函数def  fun1 (x, y):     print (' x+y=%d '  %  (x+y))     return x+yfun1 (1, 3 )   #x +y=4#----------------------------------------#默认参数 (default parameters must be all with default parameters, with C language) def fun2 (x, y  =  ' a '):     print (' x=%d, y=%s '  %  (x, y)) fun2 (2)   #x =2, y= a#----------------------------------------#当调用引用关联参数名时, order can not be fixed def fun3 (x, y, z):     print (' x=%s, y=%s, z=%s '  %  (x, Y, z) fun3 (y =  ' B ', z =  ' C ', x =  ' a ')   #x =a, y=b, z=c   incoming order with parameter name can change #------------------------ ----------------#不定长参数, the *x parameter is actually equivalent to passing in a tuple def fun4 (*x):     print (x)      for i in x:        print (' x=%s '  % i) fun4 (1,2,3,4,5) # (1, 2,  3, 4, 5) #x =1#x=2#x=3#x=4#x=5#----------------------------------------#3. Before the 0 version, the indeterminate length parameter can only have one and is placed in the last   If Def fun5 (x, *y, z): You will get an error. Version support after 3.0, \ But the last parameter of the call needs to specify how much Z equals to the z = valuedef  Fun5 (x, *y):    for i in y:         x += i    return xsum = fun5 (2, 1, 2, 3,  4, 5) print (sum)   #17 #----------------------------------------#内嵌函数, external cannot call DEF FUN6 (x):     def fun0 (y):         return y**2     print (Fun0 (x+1)) Fun6 (2)   #9 #----------------------------------------#闭包1 (constituent conditions: 1, The function returns the call of the internal function,  2, the intrinsic function uses variables in the scope of the external function (such as x)) Def fun7 (x):     def fun7_1 (y):         RETURN X*Y    RETURN FUN7_1F = FUN7 (8) f1 = fun7 ( Ten) X = f (5) Y = F1 (5) print (x)    #40print (y)    #50 # closure 2 () "#3.0 versions prior to the defect, Can only be implemented by the following function implementation, the version after 3.0 may be added nonlocal keyword declaration x is an external variable of fun8_1, similar to GLOBALDEF FUN8 ():    x  = 10    def fun8_1 ():         nonlocal  x  #3.0 later in this statement, the system knows that x is an external variable of fun8_1         x *=  x   #x试图被修改, because x belongs to an external variable of the Fun8_1 function,  and the x system in Fun8_1 is considered an internal variable of fun8_1, it will not be found, resulting in an error          return x    return fun8_1 () "Def fun8" ():     x = [10]  #列表在下方使用的时候不会被屏蔽     def fun8_1 ():         x[0] *= x[0]   #x试图被修改, since x belongs to the external variable of the Fun8_1 function,   The X system in Fun8_1 is considered an internal fun8_1Variable, it will not be found, resulting in an error         return x[0]    return  fun8_1 () Z = fun8 () print (z)   #100 #----------------------------------------#匿名函数lambda   Parameters ...: return value Fun9 = lambda x,y,z: x+y*zprint (fun9)    #7 #---------------------- ------------------#重要函数filter (the list of conditional functions, ),  filter out content that satisfies the condition to true in the list and make the object return, which can be converted to a list listt = list (Filter (Lambda x: x%2, range)) print (LISTT)   #[1, 3, 5, 7, 9]# The important function map (the conditional function,  list),  the element one by one in the list and makes the object return, which can be converted to a list listt = list (map (lambda x: x+2,  range ()) print (LISTT)   #[2, 3, 4, 5, 6, 7, 8, 9, 10,  11]#----------------------------------------#函数文档 (better to print with help) def fun10 ():     ' Add a function document here to describe the function's parameter information, function, etc. ' Def fun11 ():     '      so you can add multiple lines       Use fun.__doc__ or help to display the function document      "fun10.__doc__# here to add a function document, to describe the function of parameter information, functions, etc. fun11.__doc __# This allows you to add multiple lines \ n use fun.__doc__ or help to display the function document \NHELP (FUN10) #Help  on function fun in  module __main__:# #fun10 () #     add a function document here to describe the function's parameter information, functions, etc. help (fun11) #Help  on  function fun in module __main__:# #fun11 () #     so you can add multiple lines #      use fun.__doc__ or Help to display function documents


This article is from the "Python" blog, so be sure to keep this source http://guyuejinsheng.blog.51cto.com/9586491/1767283

Python function Usage Rollup

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.