Python 函数
函数定义:
def fun_name(arg1[= default],arg2 = [= default]...):
函数体
函数默认参数:
函数的可变参数
1、 * 元组
2、 ** 字典
函数返回:
1、返回None
2、返回元祖
3、返回多个值
lambda函数:
func = labmbda 变量1,变量2...:表达式
Generator函数:
一次产生一个数据项,并把数据项输出;
定义:
def 函数名(参数列表):
...
yield
函数定义:
def fun_name(arg1[= default],arg2 = [= default]...):
函数体
函数默认参数:
函数的可变参数
1、 * 元组
2、 ** 字典
函数返回:
1、返回None
2、返回元祖
3、返回多个值
def fun1(x):
print x
def fun2(x):
a=(x,x+1)
return a
def fun3(x):
return x,x+1
print fun1(2)
print fun2(2)
print fun3(2)
输出:
None
(2, 3)
(2, 3)
lambda函数:
func = labmbda 变量1,变量2...:表达式
print sum(1,2)
print (lambda x:-x)(1)
Generator函数:
一次产生一个数据项,并把数据项输出;
定义:
def 函数名(参数列表):
...
yield
def fun4(x):
for i in range(x):
yield i
for i in fun4(5):
print i
print '........................'
t = fun4(5)
print t.next()
print t.next()
print t.next()
print t.next()<pre code_snippet_id="1855768" snippet_file_name="blog_20160829_4_9572920" name="code" class="python">输出:<pre code_snippet_id="1855768" snippet_file_name="blog_20160829_5_9431216" name="code" class="python">0
1
2
3
4
........................
0
1
2
3