Python从函数返回值
示例
函数可以return是一个可以直接使用的值:
def give_me_five():
return 5
print(give_me_five()) #打印返回值
#出:5或保存值供以后使用:
num = give_me_five() print(num) #打印保存的返回值 #出:5
或将值用于任何操作:
print(give_me_five() + 10) #出:15
如果return在函数中遇到此函数,则将立即退出该函数,并且不会评估后续操作:
def give_me_another_five():
return 5
print('This statement will not be printed. Ever.')
print(give_me_another_five())
#出:5您还可以使用return多个值(以元组的形式):
def give_me_two_fives():
return 5, 5 #返回两个5
first, second = give_me_two_fives()
print(first)
#出:5
print(second)
#出:5没有return语句的函数隐式返回None。类似地,具有return语句但没有返回值或变量return的函数None。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短