Defined functions
def 函数名(等同于变量名):
'''对函数(工具)的描述信息'''
代码块
example:
def guess():
'''给定两个数,打印较大的数'''
x=20
y=30
if x>y:
print(x)
else:
print(y)
print(guess.__doc__)#打印注释
The characteristic function is defined: function definition procedure, only detect syntax, the code is not performed. Call phase, it will execute the code.
Three forms defined functions #
Empty function
def shopping():
pass
There are function parameters
def guess(x,y):
'''给定两个数,打印较大的数'''
if x>y:
print(x)
else:
print(y)
print(guess.__doc__)#打印注释
guess(10,20)
No-argument function
def guess():
'''给定两个数,打印较大的数'''
x=20
y=30
if x>y:
print(x)
else:
print(y)
print(guess.__doc__)#打印注释
Call functions
Call: function name ()
guess()# 调用
print(guess)#函数的内存地址
Function's return value
Return Value: returns a value (all data types)
def guess():
'''给定两个数,打印较大的数'''
x=20
y=30
if x>y:
return x
else:
return y
num=guess()
print(num)
return characteristics:
1.return returns a return value, if no return value, return None
2. There is no return, default return None
3.return function terminates, the following code is not running, when assuming a plurality of return, it will run to the end of the first, the second will not run.
4.return separated by commas, you can return multiple values, return values accepted as a tuple.
Function parameters
Katachisan
The product of stage defined function having arguments is accepted as having a descriptive sense
Location parameter
Accepts values from left to right argument
The default parameter
1. If you do not give a value, the default value; if the value when called by value, use pass
2. The default parameter must be placed behind the position parameter
Arguments
The product call stage, pass a particular parameter value, according to a specific value (so-bit data type can)
Argument position
From left to right position to pass parameter values correspond, parameter number, the number of arguments must.
Keyword argument
1. The position argument must be written before the keyword arguments.
2. The name of the parameter to pass parameter values (usage is substantially 0) - "parameter function is generally 0-3, so as not more than three.
Vararg
The variable length parameter
* Lt, all pass over all arguments received and stored in the form of a tuple
def min_10(a, b, c, *lt): # *lt,把所有传过来的实参全部接收,并且以元组的形式存储
print(lt)#(4, 5, 6, 7, -1, 9, 10, 11, 12, 13, 1, 1, 1, 1, 1, 1, 1, 11, 1)
a, b, c, d, e, f, g, h, i, j, *_ = lt
print(_)#[1, 1, 1, 1, 1, 1, 1, 11, 1]
res = min_10(1, 2, 3, 4, 5, 6, 7, -1, 9, 10, 11, 12, 13, 1, 1, 1, 1, 1, 1, 1, 11,
1, ) # *,相当于做了解压缩,也就是把lt内的元素一个一个取出来传给形参
print(res)
Variable-length argument
* Equivalent to do decompression, i.e. the elements within lt taken out one by one passed parameter
def min_10(a, b, c, d, e, f, g, h, i, j):
print(a, b, c, d, e, f, g, h, i, j)#1 2 3 4 5 6 7 8 9 10
lt = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
res = min_10(*lt) # *,相当于做了解压缩,也就是把lt内的元素一个一个取出来传给形参
print(res)