定义一个函数可以在最后加上return返回值,方便查看函数是否运行完成和返回函数的值
# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"def test(): print('test')def test1(): print('test1') return 8def test2(): print("test2") return 4,'test2',['dog','cat'],{"name":"John"}a =test()b =test1()c =test2()print(a)print(b)print(c)
运行结果
函数可以不用return,如果没有return返回值,函数返回的值为None
函数可以返回数字,字符串,列表,元组,字典,集合
如果返回多个值,则返回的值将以元组返回
return语句代表函数执行结束,函数不执行return语句后的操作
# -*- coding:utf-8 -*-__author__ = "MuT6 Sch01aR"def test(): print("Before the return") return 0 print("After the return")test()
运行结果
只执行了return语句前的语句,没有执行return语句后的语句