码迷,mamicode.com
首页 > 编程语言 > 详细

python return 及lambda函数

时间:2018-04-06 19:37:24      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:语句   执行   作用   python re   body   int()   mil   blog   对比   

return有两个作用:

1.用来返回函数的运行结果,或者调用另外一个函数。比如max()函数

>>> def fun(a,b):   #返回函数结果。
	return max(a,b)

>>> fun(6,8)
8
>>> 

2.函数结束的标志。只要运行了return,就强制结束了函数。return后面的程序都不会被执行。

>>> def fun(a,b):
	return (‘不运行后边的语句了,到此为止!‘)     #函数不在往下运行。
	if a>b:
		print(a)

		
>>> fun(8,1)        #运行到 return 后,不在运行后边的 if 语句
‘不运行后边的语句了,到此为止!‘
>>> 
>>> def fun(a,b):      #这次return 放在最后,与print() 做对比,
	if a>b:
		print(a)
		return (‘函数到此为止!‘)

	
>>> fun(8,1)
8
‘函数到此为止!‘
>>> 

如果函数中没有写return,其实函数运行结束时,默认执行了 return None。

>>> def fun(a,b):    #这里的return返回函数结果
	return (a,b)

>>> a = fun(1,2)
>>> print(a)          #验证返回的结果
(1, 2)
>>> def fun(a,b):  #这个函数里面就没有 return
	print (a,b)

	
>>> a = fun(5,6)   
5 6
>>> print(a)    #因为没有添加 return,所以返回为 None(默认的值)
None
>>> 

  

 

python return 及lambda函数

标签:语句   执行   作用   python re   body   int()   mil   blog   对比   

原文地址:https://www.cnblogs.com/longxd/p/8728467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!