标签:代码 字符串 表达式 str int() world 没有 示例 根据
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数
函数的好处:
函数的定义规则:
函数的语法:
def 函数名:
函数体
函数的示例:
def print_hello():
"""
打印hello
:return:
"""
print("hello")
根据示例来看下python的返回值:
def fun2():
msg = "hello world"
return msg
def fun3():
return 1, 2 ,3
aa = fun1()
bb = fun2()
cc = fun3()
print(aa)
print(bb)
print(cc)
# 输出结果:
# None
# hello world
# (1, 2, 3)
总结:
1.函数中如果没有return语句返回,那么python函数会默认返回None
2.函数返回值数为0,函数默认返回None;函数返回值数为1是,则返回object;返回值数大于1时,则返回的是一个tuple;
标签:代码 字符串 表达式 str int() world 没有 示例 根据
原文地址:https://www.cnblogs.com/fengyuhao/p/8906063.html