In a conditional operation, for a simple if Else statement, you can use the ternary operation to denote that:
# Normal Conditional statement if 1 = = 1: name = ' Wupeiqi ' else: name = ' Alex ' # ternary operation name = ' Wupeiqi ' if 1 = = 1 Else ' Alex '
For simple functions, there is also a simple way to express the function in the presentation method, namely: lambda expression
Lambda is just an expression, and the function body is much simpler than def.
The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.
A lambda expression acts as a function sketch. Allows the definition of a function to be embedded within the code.
# ###################### normal function ####################### definition function (normal way) def func (Arg): return arg + 1 # Execute function result = Func (123) # ###################### Lambda ###################### # definition function (lambda expression) My_lambda = lambda Arg:arg + 1 # Execute function Res Ult = MY_LAMBDA (123)
The existence of Lambda means a concise representation of simple functions
Python lambda expression simple usage