#6. Pass any number of parameters;
#在定义函数时, if the parameter name is preceded by "
", it means that you can accept any number of arguments, which are saved in a meta-Zuzhong.
#定义函数,Represents b is a meta-ancestor and can accept multiple parameters
def add (a,*b):
S=a
#用循环迭代元祖b中的对象.
For x in B:
#累加
S+=x
#返回累加的结果.
return s
#调用函数输入两个参数求和, output results.
Res=add (ON)
Print ("Two parameter summation result:", res)
#调用函数输入3个参数求和,输出结果。
Res=add (a)
Print ("Three parameter summation result:", res)
#调用函数输入5个参数求和,输出结果。
Res=add (1,2,3,4,5)
Print ("Five parameter summation result:", res)
#7. Parameters that must be passed by assignment
#python允许使用必须通过赋值传递的参数.
#B is a mutable parameter, followed by the parameter C when calling the value of the call, you must indicate the name of the parameter.
def add (A,b,c):
S=a+c
For x in B:
S+=x
return s
#调用函数, parameter C is not passed with an assignment, an error occurs.
Res=add (1,2,c=3)
Print (RES)
#正确使用方式, the parameter C must be specified when calling.
#另外, Parameters with * can also be omitted, representing the passing of an empty ancestor.
Res=add (1,c=5)
Print (RES)
#函数的嵌套定义, Python allows you to define functions inside a function.
def add (A, B):
#在函数内部定义的函数, converts the string to an ASCII sum.
def getsum (x):
S=0
For n in x:
S+=ord (N)
return s
Return Getsum (a) +getsum (b)
#调用函数, a bit similar to the meaning of recursion.
Res=add (' A ', ' B ')
Print (RES)
#lambda函数; An expression function that defines an anonymous function that assigns the function to a variable and is called by a variable. The basic format for the Lambda function definition is as follows:
#Lambda parameter table: expression
#定义lambda表达式函数, implement the function of summing two arguments and assign the function to a variable.
#定义Lambda表达式函数, the function for summing two parameters.
#并将该函数赋值给一个变量.
Add=lambda a,b:a+b
#使用函数变量进行函数调用, passing in two integers
Res=add (ON)
Print (RES)
#使用函数变量进行函数调用, passing in two strings
Res=add (' AB ', ' CD ')
Print (RES)
#lambda函数充分说明了python中的函数名就是一个变量, the variable references a function object.
#lambda函数非常适合定义简单的函数, unlike Def, a lambda function can only be an expression that calls other functions
#但不能使用python的其他语句.
#在lambad中调用其他的函数
Add=lambda A,b:ord (a) +ord (b)
Res=add (' 1 ', ' 2 ')
Print (RES)
#递归函数, a recursive function is a call to the function itself in the body of a function. For example: FAC ()
#定义函数:
def FAC (n):
If n==0:
#递归函数必须能够终止, there is a boundary condition, otherwise it will become a dead loop;
Return 1
Else
#递归调用函数本身;
return nFAC (n-1)
#调用函数;
RES=FAC (5)
Print (RES)
#函数列表, Python allows a function to be used as a list object and then called by a list index.
#使用lambda函数建立列表.
D=[lambda A,b:a+b,lambda a,b:ab]
#通过下标, the first function in the invocation list;
Res=d0
Print (RES)
#通过下标, a second function in the invocation list;
Res=d1
Print (RES)
#也可以使用def定义的函数来创建列表.
#定义求和函数;
def add (A, B):
Return a+b
#定义求阶乘函数;
def FAC (n):
If n==0:
Return 1
Else
Return N*fac (n-1)
#建立函数列表; for D, it is a list of two functions loaded, add, FAC;
D=[ADD,FAC]
#调用求和函数;
Res=d0
Print (RES)
#调用求阶乘函数;
Res=d1
Print (RES)
#3. Python also allows you to use a dictionary to establish function mappings, such as:
#通过字典的方式, build a map of the two functions already defined above.
d={"sum": Add, "seeking factorial": FAC}
#调用求和函数
Res=d "Sum"
Print (RES)
#调用求阶乘函数;
Res=d "Seeking factorial"
Print (RES)
#注意: The essence of a function list is to establish a reference to a function object in a sequence such as Ganso, list, and dictionary, and then to invoke the function since.
Python function (ii)