Functions are well-organized, reusable pieces of code that are used to implement a single, or associated function. Functions can improve the modularity of the application, and the reuse of the code, the function that you create is called the user-defined function. function definition Specification: 1. The function code block begins with a def keyword followed by the function identifier name and parentheses ().
2. Any incoming parameters and arguments must be placed in the middle of the parentheses, and the parentheses can be used to define the parameters.
3. The first line of the function statement can optionally use the document string-for storing function descriptions.
4. Function contents start with a colon and indent.
5.return [expression] End function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
function definition syntax: def function name (formal parameter): function body By default, parameter values and parameter names are matched in the order defined in the function declaration. Example: #可以不定义形参 >>> def
Show (): ... print (' Show fun ') ...>>> show () Show fun simple computational area function:>>> def
Area (height,width): ... a = height*width ... return a...>>> area (4,5) 20 function call function calls directly using the function name (), if the function name is used directly, The return is the physical address of the function, when added (), is the parameter parameters, arguments >>> def that represent the function function.
Area (height,width): ... a = height*width ... return a...>>> area (4,5) 20 in this example, height and width are parameters, is to define the structure and logic of the function used, and the following 4, 5 is the argument, is the user in the call function, the actual parameters, the number of arguments must be consistent with the formal parameters, when called, the specific values of the formal parameters according to the order of the arguments passed one by one corresponding, such as above, 4 corresponds to the width of height,5 Keyword parameters (specify parameters) use the keyword parameter to allow the order of the arguments to be inconsistent with the declaration when the function is called, as follows >>>
def area (height,width): ... a = height*width ... return a...>>>
Area (width = 5,height = 4) # We specify the value of the specific parameter when calling the function, and if no arguments are passed, the default arguments are used when the function is called by default arguments. The default parameter must be defined on the last side of all parameters >>>
def Stu (name,age,grade=3): ... print (' name is%s,age is%s,grade%s '%
(Name,age,grade)) ...>>>
Stu (' Ian ', ten) #在这里没有传入grade参数, grade is used by default =
3name is
Ian,age is 10,grade is 3>>>
Stu (' Isha ', 9,4) name is
Isha,age is 9,grade is 4 dynamic parameter # dynamic parameter # 01, add all incoming arguments to a meta-ancestor inside Def main2 (*arg): Print (Arg,type (ARG)) main2 (1,2,3,4) #动态参数02, Add all incoming arguments to a dictionary def main3 (**kwargs): Print (Kwargs,type (Kwargs)) main3 (k1=12,k2= ' Ian ') #动态参数03, the above two merge Def
Main4 (*args,**kwargs): Print (Args,type (args)) print (Kwargs,type (Kwargs)) main4 (1,2,k1=12,k2= ' Ian ') # The above formal parameters and the order of the arguments here can not be reversed, otherwise it will be an error #动态参数4, if the argument is a variable, how to assign to the dynamic parameters inside---> with *def
Main4 (*args,**kwargs): Print (Args,type (args)) print (Kwargs,type (Kwargs)) m = [1,2]n =
{' K1 ': ' K2 ': ' Ian '}main4 (*m,**n) Summary: Python's functions have a very flexible parameter pattern, both for simple invocation and for very complex parameters. The default parameter must be used immutable object, if it is a mutable object, run there will be a logic error! Note The syntax for defining mutable and keyword parameters: *args is a mutable parameter, and args receives a tuple;**kwargs is a keyword parameter, Kwargs receives a dict. Using *args and **kwargs is a Python idiom, but it can also be used with other parameter names, but it's best to use idioms. anonymous function Python uses lambda
To create an anonymous function. The so-called anonymous, meaning no longer use Def
Statement defines a function as a standard form. 1.lambda is just an expression, and the function body is much simpler than def.
The body of the 2.LAMBDA is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.
3.lambda
The function has its own namespace and cannot access parameters outside its own argument list or in the global namespace.
4. Although the lambda function appears to be only a single line, it is not equivalent to a C or C + + inline function, which is designed to call small functions without consuming stack memory to increase operational efficiency.
The syntax for the anonymous function syntax lambda function contains only one statement, as follows: Lambda [arg1
[, arg2,..... argn]]:expression Example:>>>
Su = Lambda a,b,c:a + b + c>>> su (All-in-one) 6 Return statement return is used to exit the function, using return [expression] returns an expression when the function is exited, return without argument returns none &G T;>> def
Area (height,width): ... a = height*width ... return a...>>> AR =
Area (4,5) >>>
Print (' area was ', AR) area is 20 global variables and local variables when you declare variables within a function definition, they have no relation to other variables that have the same name outside the function, that is, the variable name is
Part of the. This is called the scope of the variable. All variables are scoped to their defined blocks, starting with the point where their names are defined. Local variables: def func (x): Print (' x is ', x) x=2 print (' Changed local x to ', x) x=50func (x) print (' x is still ', X) results are: X is 50Changed Local x to 2x is still 50 use the global statement if you want to assign a value to a variable that is defined outside the function, then you have to tell Python that the variable name is not local, but
The global. We use the global statement to complete this function. Without the global statement, it is impossible to assign a value to a variable that is defined outside the function. def func (): Global x Print (' x is ', x) x=2 print (' Changed local x to ', X) X=50func () print (' x is still ', X) results for: X is 50Chang Ed local x to 2x is still 2 #global语句被用来声明x是全局的, so when we assign a value to x within a function, the value of x outside the function changes directly