关于python return 和 print 的区别

   概念上一个是 返回值   一个是打印输出  

区别一:return是结束语一般放在函数的最后,当你在return 结束后面再写一些东西是不执行的如 下

def   renshu(x,y):
     h=x+y
     print (h)
     return  h 
     print ('hello  word')


print (renshu(3,7))

执行后的结果是 Hello word 不执行。没有报错

C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/python/class/suibi.py
10
10


1 C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/python/class/suibi.py
2 10
3 10
区别二return  返回的结果可以调用在别去如做相加

def renshu(x,y):
h=x+y
print (h)
return h
print ('hello word')


print (renshu(3,7))

x= (renshu(3,7))+10
print (x)


执行结果是
C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/python/class/suibi.py
10
10
10
20

Process finished with exit code 0

 

print 仅仅只是打印没有结果
def   renshu(x,y):
h=x+y
print (h)




print (renshu(3,7))

x= (renshu(3,7))+10
print (x)

报错信息

None
  File "C:/Users/Administrator/PycharmProjects/python/class/suibi.py", line 13, in <module>
10
    x= (renshu(3,7))+10
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Process finished with exit code 1

posted @ 2016-05-22 14:59  下丶雨天  阅读(9269)  评论(0编辑  收藏  举报