2018-10-17
閱讀量:
2497
Python中print與return的區(qū)別是什么?
從概念上理解,print是打印輸出,return是返回值。區(qū)別如下:
1、return是結(jié)束語(yǔ)一般放在函數(shù)的最后,當(dāng)函數(shù)執(zhí)行return 得到返回值后,后面的語(yǔ)句就不執(zhí)行了。
------------------------------
def additive(x,y):
z = x + y
print("hello world!")
return z
print("I love Python!")
additive(1,1)
-------------------------------
hello world!
2
2、return返回的結(jié)果可以被調(diào)用。
------------------------------
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僅僅只是打印,沒有結(jié)果
------------------------------
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'






評(píng)論(0)


暫無數(shù)據(jù)
CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
0條評(píng)論
0條評(píng)論