Three-dimensional expression
Basic Syntax format
if else is false when the result
Compares a large value in two numbers and returns
#一般函数的写法
defmax2 (x, y):ifX >y:returnxElse: returnYRES=MAX2 (10,11)Print(RES)#结果x=12y=11#------------------------# ternary expressions are applied only to:# 1. The condition is established returns a value# 2. Condition not established returns a value res=x if x > y else y
Print (res)#升级版后的函数defmax2 (x, y):returnXifX > YElseyPrint(Max2 (10,11))
Recursive one, what is recursion
Recursive functions: Recursive invocation of functions, that is, in the process of function calls, the function itself is called directly or indirectly
# Call directly def foo (): Print ('fromfoo') foo () foo ()
# Indirect invocation def Bar (): Print (' frombar') foo ()def foo (): Print ('fromfoo') bar () foo ()
Second, recursion is divided into two stages, recursion and backtracking
1. Recursion: recursive function Layer-deep process is the process of recursion
2. Backtracking: The process by which a recursive function returns results after satisfying the end condition.
Summarize:
1. Recursion must have a definite end condition
2. The size of the problem should be reduced every time the next recursion is entered
3. No tail-recursive optimizations in Python
anonymous functions
What is anonymous function keyword lambda
anonymity means no name . def func (x,y,z=1): return x+y+z anonymous lambda# has the same scope as a function , but anonymity means that the reference count is 0 and is used once to release, unless it has a name func=Lambda x,y,z=1:x+y+z func (All-in-one)# It makes no sense to have a name .
Summarize:
1. The purpose of anonymity is to have no name, it doesn't make sense to assign a name to an anonymous function.
2. The parameter rules and the scope relation of the anonymous function are the same as the famous function.
3. The function body of an anonymous function should normally be an expression that must have a return value
Python full stack development: Python ternary expressions, recursion, anonymous functions