Lambda
anonymous function, the core is as an operator, processing logic only one row but with functions of the characteristics, the core for functional programming
Ternary operators
Essentially a simplified version of the if branch that satisfies the value returned by the IF condition before the condition returns the value after else
# < 100 returns FALSE, returns the value after the else (if) + < 10print (value)
Map
The mapping function, which sequentially passes the values in the Iterated object (which can be multiple) to the function, and then returns the generator (length is based on the shortest))
numbers = [1, 2, 3, 4]names = [' Bei_men_chui_xue ', ' qi_niu_yun ']numbers = map (lambda Value:value + 1, numbers) students = Map (Lambda Value:value.upper (), names) print (list (numbers)) Print (list (students))
Reduce
Decrease function (continuous calculation, the first value in the handler function is the return result of the last function execution, the second value is the value from the iteration object sequentially, and the result of the last function execution is returned)
From random import randintfrom functools Import reducenumbers = [Randint ( -10, Ten) for _ in range]print (numbers) print (s Um (numbers)) Numbers_sum = reduce (lambda one, Two:one + A, numbers) print (numbers_sum)
Filter
In order to pass an iterative object into a function sequentially, get the value returned by the function to true, and become the generator
From random import randintnumbers = [Randint ( -10, Ten) for _ in range (Ten)]print (numbers) # takes an integer greater than or equal to 0 result = Filter (lambda Value:value >= 0, numbers) print (list result)
Closed Package
function + environment variable (placed in the upper function, the upper function does not have any parameters, it cannot form a closure) (no longer affected by any parameters outside the outer function)
A function is defined within the closure schema function, the outer function returns the inner function name, and the inner function needs to refer to the outer function variable
Closure meaning: The field of the function call (the variable of the upper function) is saved, and the field variable is modified by nonlocal declaration is not the current function local variable.
Closures are the principle of the adorner implementation, and all closure problems can be solved by object-oriented classes, and the simple implementation of the sum method is chosen.
def is_jpg (name): "" " determines whether the suffix of the file is JPG, if not add the JPG" "" def wrapper (filename): If filename.endswith (name) : return filename else: return filename + name return wrapperif __name__ = = ' __main__ ': f = is_jpg (' . jpg ') print (f (' Beimenchuixue.txt ')) print (f (' beimenchuixue.jpg '))
Experience:
1. Ternary operators are essentially shorthand logic for if branches
2. Frequently used commands are programming, imperative programming includes process control, functions, branching, loops, functional programming including map reduce filter lambda, theoretically can replace all imperative programming
3. Imperative programming can simplify code logic with functional programming
4. Function closures, which essentially retain the variables of the upper function
python-Functional Programming-map reduce filter lambda ternary expression closure