The process for the python function ternary expressions with recursive function

5.11 process-oriented programming ideas

The core is a step 'process' word, that is the process of solving problems, which is to do, and then do ........ based on process-oriented programming is like designing a pipeline, is a mechanical way of thinking.

Summarizes the advantages and disadvantages: Advantages: complex flow problem, and then simplify Disadvantages: modify a stage, other stages are likely to need to make changes, indeed affect the whole body, that is, poor scalability applications: for scalability requirements low scenes

5.12 a triplet of expressions

Ternary expressions with only applies: 1, a condition is established to return the value 2, a return condition is not established value

Returns the value of the condition is satisfied if the conditions else the value returned if the condition is not satisfied
def max2(x,y):
   return x if x > y else y
print(max2(10,11))
def max2(x,y):
   if x > y:
       return x
   else:
       return y
res=max2(10,11)
print(res)

5.13 Recursive Functions

Recursion is divided into two phases 1, backtracking: Note: Be sure to meet certain conditions in the back end, otherwise infinite recursion 2, recursive

items=[1,[2,[3,[4,[5,[6,[7,[8,[9,[10,]]]]]]]]]]
def tell(l):
   for item in l:
       if type(item) is not list:
           print(item)
       else:
           tell(item)

tell(items)
def age(n):
   if n == 1:
       return 18
   return age(n-1)+2 #age(4)+2

age(5)

Summary: 1, must have a clear recursive end condition 2, in each recursive into the scale of the problem should reduce 3, no tail recursion optimization in python

Guess you like

Origin www.cnblogs.com/mylu/p/11025631.html