摘要: python允许在函数内部定义另一个函数,这种函数称为内嵌函数或者内部函数。 1、例 >>> def a(): ## a外层函数,b为内层函数 print("hello world!") def b(): print("xxxxx!") b() >>> a() hello world! xxxxx! 阅读全文
posted @ 2021-03-04 22:06 小鲨鱼2018 阅读(413) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x): def b(y): return x * y return b >>> temp = a(5) ## 内嵌函数调用外部函数的变量 >>> temp(8) 40 2、 >>> def a(): x = 5 def b(): x = x + 1 ## x在内部函数是局部 阅读全文
posted @ 2021-03-04 17:34 小鲨鱼2018 阅读(52) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(): print("fun a is running!") def b(): print("fun b is running!") b() >>> a() ## 示例中函数b是函数a的内嵌函数 fun a is running! fun b is running! >>> 阅读全文
posted @ 2021-03-04 16:42 小鲨鱼2018 阅读(119) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> x = 5 ## 全局变量 >>> def a(): x = 10 ## 局部变量 print(x) >>> a() 10 >>> x ## 函数内部修改全局变量,不能真正的修改全局变量 5 2、 >>> x = 5 >>> def a(): global x ## 在函数内部增加gl 阅读全文
posted @ 2021-03-04 16:28 小鲨鱼2018 阅读(393) 评论(0) 推荐(0) 编辑
摘要: 1、 def discount(price,rate): ## 定义函数名discount,两个形式参数price和rate sell_price = price * rate return sell_price ## 函数返回售价 price = float(input("please input 阅读全文
posted @ 2021-03-04 16:02 小鲨鱼2018 阅读(500) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(*xxx): print("total length is %d" % len(xxx)) print("the second element is :",xxx[1]) >>> a("aaa","bbb","ccc","ddd","eee","fff") total le 阅读全文
posted @ 2021-03-04 15:12 小鲨鱼2018 阅读(107) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x,y,z): ## x、y、z为形式参数 print(x * y / z) >>> a(20,2,5) ## 20,2,5为实际参数 8.0 >>> 2、 >>> def a(x,y,z): print(x * y / z) >>> a(20,2,5) ## 此处x,y, 阅读全文
posted @ 2021-03-04 14:25 小鲨鱼2018 阅读(343) 评论(0) 推荐(1) 编辑
摘要: 1、 >>> def a(x = 100, y = 20): ## 在形式参数中指定默认参数 print(x / y) >>> a() 5.0 >>> a(x = 500, y = 10) ## 传递实参 50.0 >>> a(x = 10, y = 3) 3.3333333333333335 阅读全文
posted @ 2021-03-04 11:51 小鲨鱼2018 阅读(177) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x,y,z): print(x / y + z) >>> a(10,2,4) ## 默认位置参数 9.0 >>> a(x = 2,y = 10, z = 4) ## 指定关键字参数 4.2 >>> >>> a(x = 2, y = 10,4) SyntaxError: po 阅读全文
posted @ 2021-03-04 10:54 小鲨鱼2018 阅读(212) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x,y): ## 函数文档 """ function: sum author: xxxx date: 2021.3.4 """ print(x + y) >>> a(40,80) 120 >>> print(a.__doc__) ## 获取函数文档 function: su 阅读全文
posted @ 2021-03-04 10:26 小鲨鱼2018 阅读(184) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x): ## 形参为x, 实参为50。 print(x * 100) >>> a(x = 50) 5000 阅读全文
posted @ 2021-03-04 10:00 小鲨鱼2018 阅读(354) 评论(0) 推荐(0) 编辑