乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      《Python編程快速上手——讓繁瑣的工作自動(dòng)化》讀書筆記2

       Four兄 2019-08-24

      其實(shí),寫這個(gè)是為了督促自己看書……然后 ……其實(shí)沒有然后了,人一松懈下來,就……ε=(′ο`*)))唉

      第三章 函數(shù)

      ①def語句和參數(shù)

      先舉一個(gè)簡單的例子:

      1. def hello():
      2. print('Hello World!')
      3. hello()
      4. hello()
      5. hello()
      輸出:
      Hello World!
      Hello World!
      Hello World!

      注意:一定要記得冒號(hào)(被無數(shù)次報(bào)錯(cuò),內(nèi)心崩潰,絕望)

      好了,上面這種做法很類似在C++中的這種操作
      1. #include<bits/stdc++.h>
      2. using namespace std;
      3. void hello()
      4. {
      5. cout<<'Hello World!\n';
      6. }
      7. int main()
      8. {
      9. hello();
      10. hello();
      11. hello();
      12. return 0;
      13. }

      所以,我們看到,在括號(hào)里是沒有參數(shù)的。
      如果僅僅可以這樣,那這種語言……還不如沒有……
      所以,理所當(dāng)然,括號(hào)里也可以出現(xiàn)參數(shù),舉例如下:

      1. def hello(name):
      2. print('Hello '+name+'!')
      3. hello('Alice')
      4. hello('Bob')

      輸出:
      Hello Alice!
      Hello Bob!
      ------日常歪樓-------
      對(duì)了,忘了提一點(diǎn),在Python中,與C++不同,寫在不同行的輸出在運(yùn)行的時(shí)候,輸出的內(nèi)容也會(huì)在不同行,如果要同行輸出,就需要用逗號(hào)隔開。 
      比如:
      1. print('Hello ')
      2. print('World!')

      最后得到的是:
      Hello
      World!
      這是因?yàn)閜rint()函數(shù)自動(dòng)在傳入字符串末尾添加了換行符。          
      但是        :
      設(shè)置end關(guān)鍵字參數(shù),像這樣,也可以實(shí)現(xiàn):Hello World!
      代碼如下:
      1. print('Hello ',end='')
      2. print('World!')

      同時(shí),注意,還有別的操作,像這樣:
      1. print('cats','dogs','mice')
      2. #output:cats dogs mice
      3. print('cats','dogs','mice',sep=',')
      4. #output:cats,dogs,mice
      ---------言歸正傳-------
      在上面的函數(shù)中,有一個(gè)名為name的變量當(dāng)函數(shù)被調(diào)用的時(shí)候,參數(shù)就儲(chǔ)存在這個(gè)變量中。



      ②返回值和return語句
       還是舉例說明:
       
      1. import random
      2. def getAnswer(answerNumber):
      3. if answerNumber==1:
      4. return 'It is certain'
      5. elif answerNumber==2:
      6. return 'It is decidedly so'
      7. elif answerNumber==3:
      8. return 'Yes'
      9. elif answerNumber==4:
      10. return 'Reply hazy try again'
      11. elif answerNumber==5:
      12. return 'Ask again later'
      13. elif answerNumber==6:
      14. return 'Concentrate and ask again'
      15. elif answerNumber==7:
      16. return 'My reply is no'
      17. elif answerNumber==8:
      18. return 'Outlook not so good'
      19. elif answerNumber==9:
      20. return 'Very doubtful'
      21. r=random.randint(1,9)
      22. fortune=getAnswer(r)
      23. print(fortune)

      輸出主要看運(yùn)氣……
      比如,我是這種:
      Concentrate and ask again

      return語句包括:
      return關(guān)鍵字;
      函數(shù)應(yīng)該返回的值或表達(dá)式。
      在上面的函數(shù)里函數(shù)的名字是:getAnswer,參數(shù)的名字是:answerNumber。
      1. r=random.randint(1,9)
      2. fortune=getAnswer(r)
      3. print(fortune)
      這里也可以寫成一行:
      print(getAnswer(random.randint(1,9)))

      如↑所示。



      ③None值
      None值就是沒有值,要注意首字母大寫
      None是NoneType數(shù)據(jù)類型的唯一值(在其他語言中,被稱為null、nil或undefined)



      ④局部和全局作用域
      作用域很重要,灰常重要,格外重要:
      全局作用域中的代碼不能使用任何局部變量;
      但是局部作用域可以訪問全局變量;
      一個(gè)函數(shù)的局部作用域中的代碼,不能使用其他局部作用域中的變量;
      如果在不同的作用域中,你可以用相同的名字命名不同的變量。

      1、局部變量不能在全局作用域內(nèi)使用
      比如:
      1. def spam():
      2. eggs=31337
      3. spam()
      4. print(eggs)
      報(bào)錯(cuò)信息如下:
      Traceback (most recent call last):
        File '/usercode/file.py', line 4, in <module>
          print(eggs)
      NameError: name 'eggs' is not defined

      其實(shí)也就是說,eggs變量只屬于spam()調(diào)用所創(chuàng)建的局部作用域。在程序執(zhí)行從spam返回后,該局部作用域就被銷毀了,不再有名為eggs的變量

      2、局部作用域不能使用其他局部作用域內(nèi)的變量

      比如:
      1. def spam():
      2. eggs=99
      3. bacon()
      4. print(eggs)
      5. def bacon():
      6. ham=101
      7. eggs=10
      8. spam()

      輸出:
      99
      可以很容易發(fā)現(xiàn),在bacon中對(duì)eggs的賦值被完完全全忽略了……spam和bacon是兩個(gè)局部作用域,所以bacon中的eggs與spam中的eggs的唯一關(guān)系就是名字恰好一樣……


      3、全局變量可以在局部變量中讀取
      1. def spam():
      2. print(eggs)
      3. eggs=42
      4. spam()
      5. print(eggs)

      輸出:
      42
      42
      在這里eggs就在全局變量中,所以局部作用域spam可以讀取

      4、名稱相同的局部變量和全局變量
      1. def spam():
      2. eggs='spam local'
      3. print(eggs)
      4. def bacon():
      5. eggs='bacon local'
      6. print(eggs)
      7. spam()
      8. print(eggs)
      9. eggs='global'
      10. bacon()
      11. print(eggs)

      輸出:
      bacon local
      spam local
      bacon local
      global
      這樣寫使代碼可讀性降低,所以強(qiáng)烈建議盡量不要取相同的名字。




      ⑤global語句
      舉例如下:
      1. def spam():
      2. global eggs
      3. eggs='spam'
      4. eggs='global'
      5. spam()
      6. print(eggs)
      輸出:
      spam


      如何區(qū)分局部變量和全局變量
      1、如果變量在全局作用域中使用(即在所有函數(shù)之外),它就總是全局變量
      2、如果在一個(gè)函數(shù)中,有針對(duì)該變量的global語句,它就是全局變量
      3、否則,如果該變量用于函數(shù)中的賦值語句,它就是局部變量
      4、如果該變量沒有用在賦值語句中,就是全局變量

      舉例:
      1. def spam():
      2. global eggs
      3. eggs='spam'#this is global
      4. def bacon():
      5. eggs='bacon'#this is local
      6. def ham():
      7. print(eggs)#this if global
      8. eggs=42#this is global
      9. spam()
      10. print(eggs)
      輸出:
      spam




      ⑥異常處理
      用try except語句調(diào)試,具體方法和C++里調(diào)試的方法區(qū)別不大,就是截取片段來試,醬紫。




      '''
      然后接下來,書里是一個(gè)很……的小程序,就不放上來了,或者哪天實(shí)在無聊敲一下。
      所以就到這里啦。(?ω?)
      '''


        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多