Examples Show
#Example 1:## #递归函数求和 fromTraitlets.traitletsImportInstancedefmysum (L):Print(L)if notL:return0Else: returnL[0] + mysum (l[1:])#Calling yourself call myselfsum1= MySum ([1,2,3,4])Print(SUM1)#Write an alternate scenariodefmysum1 (L):return0if notLElseL[0] + mysum1 (l[1:])Print('#改写方案1') sum1= MYSUM1 ([1,2,3,4,5,6,7])Print(SUM1)#Rewrite Scenario 2defmysum2 (L):returnL[0]ifLen (L) ==1ElseL[0] + mysum2 (l[1:]) Sum2= Mysum2 ([a])Print('Rewrite Scenario 2')Print(sum2)#Rewrite scenario 3defmysum3 (L): First,*rest =LreturnFirstif notRestElseFirst + mysum3 (rest)##python3 Extended sequence ext seqSUM3 = MYSUM3 ([1,2,3,4,5])Print('Rewrite scenario 3')Print(SUM3)## # with While loop implementationL = [1,2,3,4,5]sum2=0 whilel:sum2+=L[0] L= L[1:]Print('implemented with a while loop')Print(sum2)##用for Loop ImplementationL = [1,2,3,4,5,6]sum3=0 forXinchl:sum3+=xPrint('implemented with A for loop') Print(SUM3)#handling arbitrary structuresdefSumtree (L): Tot=0 forXinchL:if notisinstance (x,list): Tot+=xElse: Tot+=sumtree (l[1:]) returnTotl= [1,[2,[3,4],5],6,[7,8]]Print('# #任意结构')Print(Sumtree (L))##间接函数调用defEcho (message):Print(message)defIndirect (Func,args): func (args) indirect (echo,'Shixingwen') Schedule1=[(Echo,'spam! '), (Echo,'ham!') ] for(Func,args)inchSchedule1:func (args)Print('# #间接调用函数')Print(Echo.__name__)## #python function AnnotationsdefFunc (A:'spam', B: (1,3), c:float):returnA + B +CPrint('# #函数注释')Print(Func-)) Zhushi= Func.__annotations__ Print(Zhushi,'\ n') forArgsinchZhushi:Print(Args,'=', Zhushi[args])##map mapping functions in a sequenceCounter = [1,2,3,4]update= [] forIinchCounter:update.append (i+10)Print('# #for循环实现')Print(update)defInc (I):returnI +10Print('# #map mapping functions in sequences')Print(List (map (Inc,counter )))Print('# # # #lambda can also be achieved')Print(List (Map (LambdaI:i +10, Counter )))## fromFunctoolsImportReducere=reduce (Lambdax, Y:x+y, [1, 2, 3, 4, 5])Print(RE)ImportFunctoolsPrint('Query Reduce usage') Help (Functools.reduce)##或者 fromFunctoolsImportreducehelp (reduce)
The above results are as follows
[1, 2, 3, 4][2, 3, 4][3, 4][4][]10#Rewrite scenario 128Rewrite Scenario 26Rewrite scenario 315implemented with a while loop15implemented with A for loop21st##任意结构43shixingwenspam! ham!##间接调用函数Echo##函数注释6{'C': <class 'float','a':'spam','b': (1, 3)} C= <class 'float'>a=Spamb= = (1, 3)##for循环实现[11, 12, 13, 14]##map mapping functions in a sequence[11, 12, 13, 14]## # # #lambda can also be achieved[11, 12, 13, 14]15query reduce usage Help on built-inchfunction reduceinchmodule _functools:reduce (...) Reduce (function, sequence[, initial])-value Apply A function of arguments cumulatively to the items of a sequence, fromLeft -to-right, as-to-reduce the sequence to a single value. For example, reduce (Lambdax, Y:x+y, [1, 2, 3, 4, 5]) Calculates ( ((1+2) +3) +4) +5). If Initial isPresent, it isplaced before the items of the sequenceinchThe calculation, andserves as a default when the sequence isempty. Help on built-inchfunction reduceinchmodule _functools:reduce (...) Reduce (function, sequence[, initial])-value Apply A function of arguments cumulatively to the items of a sequence, fromLeft -to-right, as-to-reduce the sequence to a single value. For example, reduce (Lambdax, Y:x+y, [1, 2, 3, 4, 5]) Calculates ( ((1+2) +3) +4) +5). If Initial isPresent, it isplaced before the items of the sequenceinchThe calculation, andserves as a default when the sequence isEmpty.
Recursion, lambda, map reduce, and more in Python functions