上一页 1 ··· 352 353 354 355 356 357 358 359 360 ··· 407 下一页
摘要: python中允许在函数内定义另一个函数,这种函数称为内嵌函数或者内部函数。 1、 >>> def a(): ## 外层函数 print("hello world!") def b(): ## 内层函数 print("xxxxxxx!") return b() >>> a() hello world 阅读全文
posted @ 2021-03-05 17:04 小鲨鱼2018 阅读(199) 评论(0) 推荐(0)
摘要: 1、一般情况下,无法在函数内对全局变量进行修改 >>> x = 10 >>> def a(): x = 1000 print(x) >>> a() 1000 >>> x 10 2、利用global关键字,在函数内对全局变量进行修改 >>> x = 10 >>> def a(): global x x 阅读全文
posted @ 2021-03-05 16:36 小鲨鱼2018 阅读(4318) 评论(0) 推荐(0)
摘要: 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 阅读(776) 评论(0) 推荐(0)
摘要: 1、 python中定义在函数内部的变量称为局部变量,局部变量只能在局部函数内部生效,它不能在函数外部被引用。 def discount(price,rate): price_discounted = price * rate return price_discounted sale_price = 阅读全文
posted @ 2021-03-05 13:23 小鲨鱼2018 阅读(859) 评论(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 阅读(438) 评论(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 阅读(60) 评论(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 阅读(130) 评论(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 阅读(406) 评论(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 阅读(551) 评论(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 阅读(115) 评论(0) 推荐(0)
上一页 1 ··· 352 353 354 355 356 357 358 359 360 ··· 407 下一页