Python學(xué)習(xí)記錄(11)

1.? 函數(shù)中的局部變量和全局變量

>>> count = 3

>>> def MyFun():

global count

count = 10

print(count)

>>> MyFun()

10

>>> print(count)

10

2.? 內(nèi)嵌函數(shù)

>>> def fun1():

print('fun1()正在被調(diào)用...')

def fun2():

print('fun2()正在被調(diào)用...')

fun2()

>>> fun1()

fun1()正在被調(diào)用...

fun2()正在被調(diào)用...

3.?>>> def Fun1(x):

def Fun2(y):

return x*y

return Fun2

>>>

>>> i = Fun(8)

>>> i(5)

40

>>> Fun1(5)(8)

40

4.? 函數(shù)中的局部變量和全局變量

例1:? 列表在函數(shù)中局部和全局變量的應(yīng)用

>>> def Fun1():

? ? ? ? ? ? ? ? ? ? ? x = 5

? ? ? ? ? ? ? ? ? ? ? def Fun2():

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x *= x

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return x

? ? ? ? ? ? ?? return Fun2()

>>> Fun1()

Traceback (most recent call last):

? File "<pyshell#47>", line 1, in <module>

? ? Fun1()

? File "<pyshell#46>", line 6, in Fun1

? ? return Fun2()

? File "<pyshell#46>", line 4, in Fun2

? ? x *= x

UnboundLocalError: local variable 'x' referenced before assignment

>>> def Fun1():

? ? ? ? ? ? ? ? ?? x = [5]

? ? ? ? ? ? ? ? ?? def Fun2():

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x[0] *= x[0]

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return x[0]

? ? ? ? ? ? ? ? ? ? return Fun2()

>>> Fun1()

25

例2: " nonlocal " 關(guān)鍵字的應(yīng)用?

>>> def Fun1():

? ? ? ? ? ? ? ? ? ?? x = 5

? ? ? ? ? ? ? ? ? ?? def Fun2():

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? nonlocal x

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x *= x

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return x

? ? ? ? ? ? ? ? return Fun2()

>>> Fun1()

25

5. lambda函數(shù)的應(yīng)用

例1:

>>> def ds(x):

return 2 * x + 2

>>> ds(2)

6

>>> lambda x : 2 * x + 2

<function <lambda> at 0x0000023767B8D048>

>>> g = lambda x : 2 * x + 2

>>> g(5)

12

6.? filter(function or None, iterable) ?? 第二個(gè)參數(shù)迭代器在第一個(gè)參數(shù)中運(yùn)算,返回結(jié)果為真的可迭代對(duì)象

>>> def odd(x):

return x % 2

>>> temp = range(10)

>>> show = filter(odd, temp)

>>> list(show)

[1, 3, 5, 7, 9]

或者

list(filter(lambda x : x % 2, range(10)))

[1, 3, 5, 7, 9]

7.? 映射函數(shù) map() ? 表示將第二個(gè)可迭代對(duì)象的每一個(gè)參數(shù)帶到第一個(gè)函數(shù)中計(jì)算,結(jié)果返回每個(gè)計(jì)算后的可迭代對(duì)象

>>> list(map(lambda x : x * 2, range(10)))

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Python史上最全開(kāi)發(fā)總結(jié)(轉(zhuǎn)自靜熙老師哈哈哈) 兩本不錯(cuò)的書(shū): 《Python參考手冊(cè)》:對(duì)Python各個(gè)標(biāo)...
    春風(fēng)在抱閱讀 854評(píng)論 1 4
  • 寫(xiě)在前面的話 代碼中的# > 表示的是輸出結(jié)果 輸入 使用input()函數(shù) 用法 注意input函數(shù)輸出的均是字...
    FlyingLittlePG閱讀 3,229評(píng)論 0 9
  • 一、函數(shù) 1.函數(shù)概述 1.1認(rèn)識(shí)函數(shù) 需求:求圓的面積s = π * r ** 2c = math.sqrt(a...
    hollow_02f9閱讀 776評(píng)論 0 2
  • 第0024張卡片 常聽(tīng)人說(shuō),孩子是父母的復(fù)印件。“復(fù)印件”出了問(wèn)題,按理來(lái)說(shuō),應(yīng)該從“原件”身上找問(wèn)題,可大多數(shù)時(shí)...
    駱駝教練閱讀 586評(píng)論 0 0
  • 今天是感恩節(jié)。9:56父親正在手術(shù)中。 我以前以為手術(shù)中的等待是最難熬的,現(xiàn)在我的心中反而是最坦然的。母親的承受力...
    吳佟閱讀 256評(píng)論 1 0

友情鏈接更多精彩內(nèi)容