函数是一种工具,能够重复调用python
一、防止代码冗余框架
二、加强代码可读性函数
def 函数名(变量1,变量2):工具
""" 函数注释描述 """code
函数体class
return值变量
函数都是先定义后调用,定义时只检测语法不执行代码语法
函数名的命名规范跟变量名的命名规范相同命名
一、直接调用 函数名() 例如:index()数据
二、从新赋值函数名
f = 函数名
f()
三、函数当参数传入函数中
index(a,index())
函数都是先定义后调用,定义时只检测语法不执行代码
是一个函数结束的标志,函数体代码只要运行到return则函数执行结束
一、不用写返回值return,默认返回值是None
def index(): print("hello word") print(index()) >>> hello word >>> None
二、只写return只有结束函数的效果 ,返回值是None
def index(): print("hello word") return print(index()) >>> hello word >>> None
三、写return None也是结束函数的效果跟只写return相同,返回值是None
四、return返回一个值
能够将返回结果当作一个变量使用
def index(a,b): if a>b: return a else: return b print(index(1,3))
五、return返回多个值
1. 将返回值的多个值默认存入元组返回 2. 函数的返回值不想被修改 3. 能够return+数据,本身指定
def func1(): return 1, "2" print(func1()) >>>(1, '2') def func(a, b, c, d, e): return [a, b, c, d, e] print(func(a, b, c, d, e)) >>> [1, 2, '3', [4, 5], {'name': 'sean'}]