1, three-dimensional expression
#What is ternary expression ternary: three elementsx=1y=2ifX>y:Print(x)Else : Print(y)#becomes ternary expression:Hhh=xifX>yElseyPrint(HHH)#Simple and clear————————————————————————————————————defmax2 (x, y):ifX>y:returnxElse : returnyPrint(Max2 (1,5))#becomes ternary expression:defmax2 (x, y):returnXifX>yElseyPrint(Max2 (1,5))#Can see the ternary expression can be output with a line of the result is very simple!!!!
2. List Deduction formula
l=[1,25,35,60,85,96]l_new=[]l_new2=[] forIinchL:ifI >50: L_new.append (i)Else: L_new2.append (i)Print(l_new)Print(L_NEW2)#become list-derivedl=[1,25,35,60,85,96]x=[i forIinchLifI < 50]y=[i forIinchLifI < 50]Print(y)Print(x)
3. Generator expression
# change the list's [] to () is the generator expression for in rangeprint(x.__next__())print(x.__next__ ())print(next (x))# you can see x.__next__ () =next (x)
These three expressions can be seen as the function of making the code more neat and simple and convenient
python--function 20, ternary expression, list derivation, generator expression