1 #a parameter,2 defShow (ARG):3 Print(ARG)4Show"KKKKK"#参数可以是列表, dictionaries, etc., as long as it's an element.5 6 #two parameters7 defShow (agr,xxx):8 Print(agr,xxx)9Show"HHHH", 77777)#If you write only one parameter, you will get an errorTen One #default parameters, default parameters must be placed behind, that is, cannot be placed in the first place A defShow (a1,a2=999): - Print(A1,A2) -Show (111)#default parameter, when executing a function, the parameter can be not lowercase (the execution result will output the default value) theShow (111,888) - - #Specifying Parameters - defShow (A3,A4): + Print(A3,A4) -Show (a4=123,a3=999)#This will not be executed in sequence. + A #Execution Result: at KKKKK -HHHH 77777 -111 999 -111 888 -999 123
Second, dynamic parameters:
1 # 2 def Show (*< Span style= "color: #000000;" >arg): 3 print (Arg,type (ARG)) 4 Show (11,22,33,44,55,66) # plus an asterisk parameter, the output puts the actual parameters in a tuple 5 # execution result: 7 (one, one, $, a, a, $) < class tuple ;
1 2. Shape to take two asterisks, output as dictionary2 defShow (* *Arg):3 Print(Arg,type (ARG))#add two asterisk parameters, the output will put the actual parameters in a dictionary.4Show (n1=78,uu=123,bb=999)#the actual parameter writing format, according to the dictionary wording5 6 #Execution Result:7{'N1': 78,'UU': 123,'BB': 999} <class 'Dict'>
1 #3. Combined use, must first tuple after dictionary2 defShow (*args,**kwargs):#In conjunction, formal parameters must also be written with a star, followed by the two-star parameter3 Print(Args,type (args))4 Print(Kwargs,type (Kwargs))5Show (11,22,33,44,aa="SDF". B ="456")#joint use, ask to write a star, the back of the two stars, or will error6 7 #Execution Result:8(One, one, one, one) <class 'tuple'>9{'AA':'SDF','BB':'456'} <class 'Dict'>
1 #4. If the actual parameter is assigned a value variable, the variable is placed in the tuple as an element2 #If you want the original format output, you need to add 1 asterisks or 2 asterisks before the variable3 defShow (*args,**kwargs):#In conjunction, formal parameters must also be written with a star, followed by the two-star parameter4 Print(Args,type (args))5 Print(Kwargs,type (Kwargs))6 #Show (11,22,33,44,aa= "SDF", bb= "456") #联合使用时, ask to write a star in front, write two stars behind, or will error7Li = [11,22,33,44,55]8DIC = {"N1": 44,"N2":"DSF"}9Show (Li,dic)#Put Li and dic as 2 elements into a tuple, the dictionary is emptyTenShow (*li,**dic)#If you want to output the value in the original format, add a star or add 2 stars to the corresponding One A #Execution Result: -([11, 22, 33, 44, 55], {'N1': 44,'N2':'DSF'}) <class 'tuple'> -{} <class 'Dict'> the(One, one, one, one, one) <class 'tuple'> -{'N1': 44,'N2':'DSF'} <class 'Dict'>
Application:
1 #2S1 ="{0} is {1}"3result = S1.format ("Yaom","345")#Normal string formatting assignment4 Print(Result)5 6S1 ="{0} is {1}"7Li = ["Yao","345"]8result = S1.format (*li)#convert output with list9 Print(Result)Ten OneS1 ="{Name} is {acter}" Aresult = S1.format (name="Liug", acter="SBT")#Normal string format assignment - Print(Result) - theS1 ="{Name} is {acter}" -DIC = {"name":"Liu","Acter":"SBT"}# -result = S1.format (**dic)#convert output with dictionary - Print(Result) + - #Execution Result: +Yaom is345 AYao is345 atLiug isSBT -Liu isSBt
Lambda expression:
1 #lambda expressions, simple function Expressions2 deffunc (a):3b = a+14 returnb5result = Func (4)6 Print(Result)7 #lambda expressions, simple representations of simple functions (and Li = [] are expressed as Li = List ())8Func =LambdaA:a+1#just one line to show it, no skip.9 #Create form parameter aTen #function content, a+1 and return the result OneRET = func (99) A Print(ret) - - #Execution Result: the5 -100
Python function parameter +lambda expression