First, define the function
Define a function to use
def
The statement, write down the function name, parentheses, the arguments in parentheses, and the colon :
, and then write the function body in the indent block, returning the function's return value with a return
statement. If there is no return
statement, the result is returned after the function is executed, except that the result is None
:
def Add (A, b ): return A +bprint add
Define an empty function to use pass
def NOP (): Pass
When defining functions, it is necessary to determine the number of function names and parameters;
If necessary, the data type of the parameter can be checked first, and the data type check can be implemented with built-in functions isinstance
.
Second, function return value
The function body can return
return the function result at any time;
Automatic when the function is finished and there are no return
statements return None
.
A function can return multiple values at the same time, essentially a tuple.
def Test (A,b,rank): C=a+rank d=b+rank return c,dc,d= Test (2,3,5)print cprint dt=test (5,10,1) Print t
third, function parameters
① Default Parameters
def Power (x,n=2): R=1 while (n>0): R=r*x n=n-1 return rprint power (2)>>4print power (2,10) >>1024
When setting default parameters, there are a few things to note:
First, the required parameter is in front, the default parameter is after;
The second is that the default parameter must point to the immutable object.
② variable Parameters
Variable parameters allow you to pass in 0 or any of the parameters that are automatically assembled as a tuple when the function is called. When you define a mutable parameter, you need to asterisking the number * before the variable name.
def sum (*numbers): s=0 for in numbers: s=s+x* x return s print sum (1,2,3,4,5)>>>55
If the parameter is a list or turple, it needs to be called at the asterisking number before the variable name.
list=[1,2,3,4,5]print sum (*list) >>>55t= (1,2,3,4,5) Print sum (*t) >>>55
③ keyword Parameters
The keyword parameter allows you to pass in 0 or any parameter with parameter names that are automatically assembled inside the function as a dict.
defShow (name,age,**param):Print "name", Name," Age", Age,paramshow ('Liu', 20,city='Beijing', job='Teacher') DiC={' City':'Beijing','Job':'Teacher','Home':'Hubei'}show ('Zhang', 30,**dic)
Note that the order of the parameter definitions must be: required, default, variable, and keyword parameters.
④ Summary
The default parameter must be used immutable object, if it is a mutable object, run there will be a logic error!
Be aware of the syntax for defining mutable parameters and keyword parameters:
*args
is a variable parameter, and args receives a tuple;
**kw
is a keyword parameter, kw receives a dict.
How to pass in the syntax of a variable parameter and a keyword parameter when calling a function:
Variable parameters can be directly passed func(1, 2, 3)
in:, you can first assemble a list or a tuple, and then pass in *args
: func(*(1, 2, 3))
;
Keyword parameters can be directly passed in: func(a=1, b=2)
, you can first assemble dict, and then pass in **kw
: func(**{‘a‘: 1, ‘b‘: 2})
.
Iv. anonymous Functions
The Lambda keyword is used in Python to define anonymous functions.
For example, the string collection elements are sorted by length.
s=['hello','World','aa' S.sort (Key=Lambda x:len (x))print s run result: ['AA ' "Hello""World"
v. Function currying
The function of curry is a fixed function of several parameters, so as to generalize a number of functions of different meanings. This requires the use of the partial function of the built-in functools module. For example:
from Import Partial def Addnumber (A, b ): return A +baddNumber10=partial (addnumber,10) addNumber20=partial (addnumber,20) print# runresultprint addNumber20 (+)# Run result 120
Python "4"-functions