A matching function parameters
When a certain number of parameters 1.1
Not necessarily the number of parameters 1.2
Defining a function, using the parameters tuples DETAILED front variable is incremented by *, such as:
def func(*score): return sum(score)/len(score)
NOTE: The parameter tuple defined function, the call can not be used tuples can be added as required before tuples *, such as :
score=(1,2,3,4)
data=func(*score)
Note: You can also use the parameter dictionary table, use the tuple similar, slightly different specific manner, before the need to add a variable **, using as parameters the name defined dictionary, then you need to add * unpack
2 lambda expression
2.1 The basic format
lambda parameter 1, parameter 2 ...: Expression
2.2 lambda is an expression, you need to assign it to a variable to use
2.3 lambda expressions poor readability, use of a lambda expression is convenient nesting aspect, additional functions using fewer, and more easy way so that the expression
3 in the preparation of key-value pairs in the dictionary table value of the function name may be used, thereby simplifying the code
def hello_chinese(name): print(‘你好’,name) def hello_english(name): print(‘hello’,name) operation={‘c’:hello_chinese,’e’:hello_english} while True: name=input(‘请输入姓名:’) if name == stop: break language=input(‘c=>中文 e=>英文’) (operation.get(language))(name)
4 function advanced tools
4.1 map function
The basic format:
map (function, iterables )
example:
def add(a): return a+5 l=list(range(1,6)) res=list(map(add,l))
4.2 filter function
The basic format:
Filter (function iterables )
example:
def jud(a): return a%2 == 0 l=list(range(1,8)) res=list(filter(jud,l))