Home >Backend Development >Python Tutorial >Detailed explanation of the use of Python functions
1. Basic definition of function
def 函数名称(参数) 执行语 return 返回值
def: Keyword to define the function;
Function name: As the name suggests, It is the name of the function. It can be used to call the function. Keywords cannot be used to name it. It is best to name it with the English name of the function. Camel case and underline methods can be used;
Parameters: used to give The function provides data, with distinction between formal parameters and actual parameters;
Execution statement: also called function body, used to perform a series of logical operations;
Return value: After executing the function, return The data given to the caller defaults to None, so when there is no return value, you do not need to write return.
2. Ordinary parameters of the function
The most direct one-to-one relationship parameters, such as:
def fun_ex(a,b): #a,b是函数fun_ex的形式参数,也叫形参 sum=a+b print('sum =',sum) fun_ex(1,3) #1,3是函数fun_ex的实际参数,也叫实参#运行结果sum = 4
3. Default parameters of the function
Define a default value for the parameter. If no parameters are specified when the function is called, then The function uses default parameters, which need to be placed at the end of the parameter list, such as:
def fun_ex(a,b=6): #默认参数放在参数列表最后,如b=6只能在a后面 sum=a+b print('sum =',sum) fun_ex(1,3) fun_ex(1)#运行结果sum = 4sum = 7
4. Dynamic parameters of the function
There is no need to specify whether the parameter is a tuple or dictionary, the function automatically converts it into a tuple or dictionary, such as:
#转换成元组的动态参数形式,接受的参数需要是可以转成元组的形式,就是类元组形式的数据,如数值,列表,元组。 def func(*args): print(args,type(args)) func(1,2,3,4,5) date_ex1=('a','b','c','d') func(*date_ex1) #运行结果 (1, 2, 3, 4, 5) <class 'tuple'> ('a', 'b', 'c', 'd') <class 'tuple'> 动态参数形式一
#转换成字典的动态参数形式,接收的参数要是能转换成字典形式的,就是类字典形式的数据,如键值对,字典 def func(**kwargs): print(kwargs,type(kwargs)) func(a=11,b=22) date_ex2={'a':111,'b':222} func(**date_ex2) #运行结果 {'b': 22, 'a': 11} <class 'dict'> {'b': 222, 'a': 111} <class 'dict'> 动态参数形式二
#根据传的参数转换成元组和字典的动态参数形式,接收的参数可以是任何形式。 def func(*args,**kwargs): print(args, type(args)) print(kwargs,type(kwargs)) func(123,321,a=999,b=666) date_ex3={'a':123,'b':321} func(**date_ex3) #运行结果 (123, 321) <class 'tuple'> {'b': 666, 'a': 999} <class 'dict'> () <class 'tuple'> {'b': 321, 'a': 123} <class 'dict'> 动态参数形式三
5. The return value of the function
When running a function, you generally need to get some information from it. In this case, you need to use return to get the return value, such as:
def fun_ex(a,b): sum=a+b return sum #返回sum值 re=fun_ex(1,3) print('sum =',re) #运行结果 sum = 4
6.lambda The expression
is used to express simple functions, such as:
#普通方法定义函数 def sum(a,b): return a+b sum=sum(1,2) print(sum) #lambda表达式定义函数 myLambda = lambda a,b : a+b sum=myLambda(2,3) print(sum) #运行结果 5
7. Built-in functions
1) Built-in functions List
Built-in Functions | ||||
---|---|---|---|---|
<span class="pre">abs()</span> |
<span class="pre">dict()</span> |
<span class="pre">help()</span> |
<span class="pre">min()</span> |
<span class="pre">setattr()</span> |
<span class="pre">all()</span> |
<span class="pre">dir()</span> |
<span class="pre">hex()</span> |
<span class="pre">next()</span> |
<span class="pre">slice()</span> |
<span class="pre">any()</span> |
<span class="pre">pmod()</span> |
<span class="pre">id()</span> |
<span class="pre">object()</span> |
<span class="pre">sorted()</span> |
<span class="pre">ascii()</span> |
<span class="pre">enumerate()</span> |
<span class="pre">input()</span> |
<span class="pre">oct()</span> |
<span class="pre">staticmethod()</span> |
<span class="pre">bin()</span> |
<span class="pre">eval()</span> |
<span class="pre">int()</span> |
<span class="pre">open()</span> |
<span class="pre">str()</span> |
<span class="pre">bool()</span> |
<span class="pre">exec()</span> |
<span class="pre">isinstance()</span> |
<span class="pre">ord()</span> |
<span class="pre">sum()</span> |
<span class="pre">bytearray()</span> |
<span class="pre">filter()</span> |
<span class="pre">issubclass()</span> |
<span class="pre">pow()</span> |
<span class="pre">super()</span> |
<span class="pre">bytes()</span> |
<span class="pre">float()</span> |
<span class="pre">iter()</span> |
<span class="pre">print()</span> |
<span class="pre">tuple()</span> |
<span class="pre">callable()</span> |
<span class="pre">format()</span> |
<span class="pre">len()</span> |
<span class="pre">property()</span> |
<span class="pre">type()</span> |
<span class="pre">chr()</span> |
<span class="pre">frozenset()</span> |
<span class="pre">list()</span> |
<span class="pre">range()</span> |
<span class="pre">vars()</span> |
<span class="pre">classmethod()</span> |
<span class="pre">getattr()</span> |
<span class="pre">locals()</span> |
<span class="pre">repr()</span> |
<span class="pre">zip()</span> |
<span class="pre">compile()</span> |
<span class="pre">globals()</span> |
<span class="pre">map()</span> |
<span class="pre">reversed()</span> |
<span class="pre">__import__()</span> |
<span class="pre">complex()</span> |
<span class="pre">hasattr()</span> |
<span class="pre">max()</span> |
<span class="pre">round()</span> |
|
<span class="pre">delattr()</span> |
<span class="pre">hash()</span> |
<span class="pre">memoryview()</span> |
<span class="pre">set()</span> |
The above is the detailed content of Detailed explanation of the use of Python functions. For more information, please follow other related articles on the PHP Chinese website!