直接贴笔记 :
#!/usr/bin/env python # coding:utf-8 # 定义函数时要写成良好的注释习惯 通常用三个单引号 def test(x): ''' 计算一个y=2*x+1 :param x: 整型 :return: 整型 ''' y = 2 * x + 1 return y # print(test) print(test(4)) ### 本次课参考 http://www.cnblogs.com/linhaifeng/articles/6113086.html#label1 def test(): ## 后定义的函数将覆盖前面的函数 ''' 测试函数 :return: ''' y = 33*2 return y print(test()) """ python中函数定义方法: def test(x): "The function definitions" x+=1 return x def:定义函数的关键字 test:函数名 ():内可定义形参 "":文档描述(非必要,但是强烈建议为你的函数添加描述信息) x+=1:泛指代码块或程序处理逻辑 return:定义返回值 调用运行:可以带参数也可以不带 函数名() """
过程其实就是没有返回值的函数:
#!/usr/bin/env python # coding:utf-8 # 参考 二 为何使用函数 http://www.cnblogs.com/linhaifeng/articles/6113086.html#label1 ''' def 发送邮件(内容) # 发送邮件提醒 连接邮箱服务器 发送邮件 关闭连接 while True: if cpu利用率 > 90 %: 发送邮件('CPU报警') if 硬盘使用空间 > 90 %: 发送邮件('硬盘报警') if 内存占用 > 80 %: 发送邮件('内存报警') ''' # 总结使用函数的好处: # # 1.代码重用 # # 2.保持一致性,易维护 # # 3.可扩展性 ########### 过程 就是没有返回值的函数 没有return def test01(): msg = 'test 01' print(msg) def test02(): msg = 'hello test02' print(msg) return msg def test03(): msg = 'test 03' print(msg) return 0,10,'hello',['alex','lb'],{'WuDaLang':'lb'} def test04(): msg = 'test 04' print(msg) return {'WuDaLang':'ssb'} t1 = test01() t2 = test02() t3 = test03() t4 = test04() print(t1) print(t2) print(t3) print(t4, type(t4))