上一页 1 ··· 312 313 314 315 316 317 318 319 320 ··· 367 下一页
摘要: 1、一般情况下,在函数内不能修改全局变量 >>> x = 10 ## 全局变量 >>> def a(): x = 1000 ## 在函数内修改全局变量x print(x) >>> a() ## 仍然输出局部变量x 1000 >>> x ## 全局变量x依然为10 10 2、使用global关键字在函 阅读全文
posted @ 2021-03-05 13:33 小鲨鱼2018 阅读(726) 评论(0) 推荐(0) 编辑
摘要: 1、 python中定义在函数内部的变量称为局部变量,局部变量只能在局部函数内部生效,它不能在函数外部被引用。 def discount(price,rate): price_discounted = price * rate return price_discounted sale_price = 阅读全文
posted @ 2021-03-05 13:23 小鲨鱼2018 阅读(790) 评论(0) 推荐(0) 编辑
摘要: 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 ··· 312 313 314 315 316 317 318 319 320 ··· 367 下一页