热线电话:13121318867

登录
2018-10-17 阅读量: 2195
Python中print与return的区别是什么?

从概念上理解,print是打印输出,return是返回值。区别如下:
1、return是结束语一般放在函数的最后,当函数执行return 得到返回值后,后面的语句就不执行了。

------------------------------
def additive(x,y):
z = x + y
print("hello world!")
return z
print("I love Python!")
additive(1,1)
-------------------------------
hello world!
2


2、return返回的结果可以被调用。

------------------------------
def additive(x,y):
z = x + y
print("I love Python!")
return z
print("I can't print!")
------------------------------
print (additive(1,1))
x= (additive(1,1))+10
print (x)
------------------------------
I love Python!
2
I love Python!
12


3、print仅仅只是打印,没有结果

------------------------------
def additive(x,y):
z = x + y
print(z)
------------------------------
print (additive(1,1))
x= (additive(1,1))+10
print (x)
------------------------------
2
None
2

TypeError Traceback (most recent call last)
<ipython-input-16-1f9bcb00fbf9> in <module>()
1 print (additive(1,1))
----> 2 x= (additive(1,1))+10
3 print (x)

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
82.3468
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子