Python functions
Tissue function is good, reusable, code segments used to implement a single, or the associated function. Function module of the application can be improved, and code reuse. Python provides many built-in functions, such as print (). You can also create your own functions, which are called user-defined functions.
The definition of a function
- In function block def beginning keyword, followed by the function identifier name and parentheses () .
- Any incoming parameters and arguments must be placed in the middle of parentheses. Between parentheses may be used to define the parameters.
- The first statement function can be selectively used documentation string - for storing function instructions.
- Function contents colon starting and retracted .
- return [Expression] End function, selectively returns a value to the caller. Return without an expression equivalent to return None.
grammar:
DEF functionName (Parameters): " function _ Document string " function_suite return [expression The]
Example:
# Create a function >>> DEF MyFirstFunction (): Print ( ' ! This is the first function I created ' ) Print ( ' ! Tan baby ' ) # call the function >>> MyFirstFunction () This is the first I've created functions! Tan Baby!
Function parameters:
In python, belong to the object type, the variable type is not:
a=[1,2,3] a="Runoob"
PS: the above code, [1,2,3] type is List, "Runoob" is of type String, and the variable is not a type, she is just an object reference (a pointer) can be List type object, you can point to a String object.
# If you set the function parameters when calling need to fill out the parameter value, or will be error >>> DEF MySecondFunction (name): Print (name + ' the I Love by You ' ) >>> MySecondFunction () Traceback (MOST recent Results Last Call ): File " <pyshell # 10> " , Line. 1, in <Module1> MySecondFunction () TypeError: MySecondFunction () Missing . 1 Positional required argument: ' name ' >>> MySecondFunction ( ' tanbaobao ' ) tanbaobaoI by You Love
#Define add function
>>> def add(num1,num2):
result = num1 + num2
print(result)
>>> add(2,3)
5
Parameter (parameter) and arguments (argument):
>>> DEF MySecondFunction (name): ' function definition process is called parameter name '
# just as he forms, represents a parameter to occupy a position
Print ( ' passed in ' + name + ' call argument, because he specific parameter values are ' ) >>> MySecondFunction ( ' tanbaobao ' ) ' tanbaobao is passed in argument, because he is the specific parameter value '
Keyword arguments:
关键字参数和函数调用关系紧密,函数调用使用关键字参数来确定传入的参数值。使用关键字参数允许函数调用时参数的顺序与声明时不一致,因为 Python 解释器能够用参数名匹配参数值。
如果没有加上关键字,则会按输入的内容进行索引,加上则按照关键字索引,会按参数上的顺序输出:
默认参数:
默认参数即定义了默认值的参数。调用函数时,默认参数的值如果没有传入,则被认为是默认值。
不定长参数(收集参数,可变参数):
需要一个函数能处理比当初声明时更多的参数。这些参数叫做不定长参数,和上述2种参数不同,声明时不会命名。加了星号(*)的变量名会存放所有未命名的变量参数。
def functionname([formal_args,] *var_args_tuple ): "函数_文档字符串" function_suite return [expression]
PS:如果既有不定长参数,又有其他参数,建议将其他参数定义为默认参数,这样能减少出错。
# 定义可变参数,还有其他的参数 >>> def test(*params,other): print('参数长度:',len(params)) print('第二个参数:',params[1]) # 调用的时候如果忘记设置其他参数的值会报错 >>> test(1,'谭酱',3.14,5,6,7,8) Traceback (most recent call last): File "<pyshell#58>", line 1, in <module> test(1,'谭酱',3.14,5,6,7,8) TypeError: test() missing 1 required keyword-only argument: 'other' # 最好的建议是将其他参数设置为默认参数,这样就算忘记传参数,也不会报错 >>> def test(*params,other=2): print('参数长度:',len(params),other) print('第二个参数:',params[1]) >>> test(1,'谭酱',3.14,5,6,7,8) 参数长度: 7 2 第二个参数: 谭酱