12345678 |
# General conditions Statement if 1 = = 1: name = ' Wupeiqi ' else : &NBSP;&NBSP;&NBSP;&NBSP; name = ' Alex ' # ternary operations ' Wupeiqi ' if < Code class= "Bash plain" >1 = = 1 else |
For simple functions, there is also an easy way to express that: lambda expression
123456789101112131415 |
# ######### ############# normal function ###################### # define function (Normal mode) def func (ARG): &NBSP;&NBSP;&NBSP;&NBSP; return arg + 1 # execute function result = func (123) Code class= "Bash Spaces" > # ###################### Lambda ########### ########### # define function (lambda expression) my_lambda = lambda Arg:arg + 1 < Code class= "Bash comments" ># execute function result = MY_LAMBDA (123) |
The existence of Lambda means a concise representation of simple functions
Built-in function two
First, Map
Iterates through the sequence, operates on each element of the sequence, and finally gets a new sequence.
Li = [one, one, 33]new_list = map (lambda a:a + li)
Li = [one, 33]SL = [1, 2, 3]new_list = map (lambda A, b:a + B, Li, SL)
Second, filter
Filter the elements in a sequence, and finally get a sequence that matches the criteria
Li = [one, one, 33]new_list = filter (lambda arg:arg > P, li) #filter第一个参数为空, the original sequence will be obtained
Third, reduce
Cumulative operation for all elements within a sequence
Li = [one, three, 33]result = reduce (lambda arg1, Arg2:arg1 + Arg2, Li) # The first argument of reduce, the function must have two parameters # reduce the second parameter, to loop the sequence # reduce The third parameter, the initial value
Python lambda expression simple usage