#-*-config=utf-8-*-#################################### function ##################################defFunc_02 (A, B):#a B: formal parameters ifA>B:Print("a greater than B") Else: Print("B is greater than or equal to a"); Func_02 (2,4);#2 4: Actual parameters###################################### parameter Set default value #########################################defMashine (x=0,y=0):#x=0 = Set Default value Print(X,"+"Y"calculation Result:", x+y); Mashine ();#using the default value of 0 + 0 calculation results: 0Mashine (21,34);#value 21 + 34 calculation Result:Mashine (21);#do not specify parameter 21 + 0 calculation result:Mashine (y=21);#Specify parameter value 0 + 21 calculation result:######################################### #变量的作用域 #########################################x="I am Global var";#Global Variablesdeffunc_03 (): b= 200;#Local Variables Print(x,b); func_03 ();#using global to force variables inside a function to be declared as global variable functions must first be calledm="c-d-e";deffunc_04 ():Globalm; M="a-b-c"; Print(m); Func_04 ();#a-b-cPrint(m);#The a-b-c function must first call the###################################### #函数的返回值 ######################################1. Return returns the function execution result and terminates the function#2. Default return Nonedeffunc_05 (x, y):Print(x+y); returnx+Y;sum=func_05 (12,23);#results of receive function processingPrint(sum);
# -*-config=utf-8-*-##################################### Multi-type parameter ####################### #Print("%s:%s"% ('A','B'));#formatted output A:BArr= ('a','b','C');#Meta-groupList=list (range (10));#Meta-groupmap={"a":"A","b":"B","C":"C"};#Dictionary#to set a tuple as a parameterdeffunc_01 (x, Y, z):Print(x, y, z); func_01 (*arr);#a b c#Default ParametersdefFunc_02 (name="Zhang San", age=12): Print("Name:%s"%name); Print("Age :%s"%Age ); func_02 (); #Use the default value when it's not worth it. Name: Zhang SanFunc_02 ("John Doe", 30);#Name: John Doe#Pass Dictionary Letter amount parameter corresponds to the dictionary key valueFunc_02 (age=50,name="Kb");#name:kb age:50map2={" Age": 40,"name":"Harry"};func_02 (**MAP2);#Name: Harry Age:40map2[" Age"]=41; func_02 (**MAP2);#age:41############################## #处理传递的多余参数 #########################*args tuples Store extra argumentsdefFUNC_03 (x,*args):Print(x); Print(args); Func_03 (1);#1 () tupleFUNC_03 (1,"a","C", 4);#1 (' A ', ' C ', 4) tuples#storing Key=value type superfluous arguments in a dictionarydefFunc_04 (x,*agrs,**Kwargs):Print(x); Print(AGRS); Print(Kwargs); Func_04 (2,"F","D","H", a="A", c="C");#2 (' F ', ' D ', ' H ') {' C ': ' C ', ' A ': ' A '}Func_04 (2,"F","D","H", x="A", c="C");#this is problematic because x passes multiple times.
#################################### #lambda (anonymous) function ####################################A lambda function is a minimal function that quickly defines a single line, which is borrowed from Lisp and can be used wherever a function is needed.#one of the operators that Python uses to support assigning a function to a variable is returned by default, so you don't have to add the return keyworddeffunc_05 (x, y):returnx*y;g=LambdaX,y:x*y;#t defines an anonymous function by lambda to pay GPrint(g);#<function <lambda> at 0x000000000104c6a8> returns a function objectPrint(g (12,34));#408#reduce in Python#the reduce built-in function in Python is a two-dollar operation function that uses all the data in a data collection (linked list, tuple, and so on) to do the following:#Use the function func (which must be a two-dollar operation function) passed to reduce to first operate on the 1th, 2 data in the collection,#The resulting result is then calculated with the third data using the Func () function, and finally a result is obtained. #after Python 3.0.0.0, reduce is no longer in the built-in function, you need to use it from the Functools import reduce fromFunctoolsImportreduce;#ask for factorial 5*4*3*2*1 of 5deffunc_06 (x, y):returnx*Y;FH=list (Range (1,6));Print(FH);Print(Reduce (FUNC_06,FH));# -Print(Reduce (LambdaX,Y:X*Y,FH));#120 working with lambda functions
Python Learning functions