Python基礎教程筆記——第6章:抽象(函數)

來源:互聯網
上載者:User

標籤:

(1)計算裴波那契數列:

1 fbis=[0,1]2 num=int(input("please input the number"))3 for i in range(num-2):4     fbis.append(fbis[-2]+fbis[-1])5 print(fbis)      

6.3 建立函數

callable():函數可以確定函數是否可以被調用

最簡單的例子:

 

1 def hello(name):2     return "hello"+name+"are you OK?"

 

輸入個數,輸出裴波那契數列的函數

 1 def fbis(num): 2     if num<=0: 3         return "input error" 4     if num==1: 5         return [0] 6     if num==2: 7         return [0,1] 8     f=[0,1] 9     for i in range(num-2):10         f.append(f[-1]+f[-2])11     return f

記錄函數:給函數寫說明文檔和查看函數的說明文檔

 

1 def hello(name):2     ‘this is a print function‘#函數說明文檔作為字串3     print("hello "+name)4 5 #查看函數文檔7 >>> hello.__doc__8 ‘this is a print function‘

 

6.4 函數參數

函數的參數和C,C++一樣也是值傳遞

 

Python基礎教程筆記——第6章:抽象(函數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.