Python函數基礎

來源:互聯網
上載者:User

標籤:bsp   技術分享   something   def   python   col   函數基礎   圖片   rgs   

函數是將程式中的一段通用代碼封裝起來,起個名字,程式的其他地方可以方便的調用

函數的定義

def  函數名(參數1,參數2...)

     do something...

    return 值/對象、函數等

 

#coding:gbkdef python_class(n):    print("周六有%s位學習Python" % n)    return Truepython_class(20)

 

參數類型

1:普通
2:預設參數
3:可變長參數 
#coding:gbkdef test_args(a,b=1):    print(a,b)    print(a+b)    test_args(3)test_args(3,2)

 

 

可變長參數: 調用函數的時候,可以傳入任意形式任意個數的參數。

 

*  代表元祖,會把所有沒有指定key的參數,把這一類參數放到一個元組中去。

** 代表的是字典  會把所有指定key的參數,放到字典當中。

 

*args  :    tuple

**kwargs :  就是當你傳入key=value是儲存的字典

 

#coding:gbkdef test_args(*args,**kwargs):    print(args,kwargs)    test_args(3,5,a=1,b=2)

函數的傳回值

#coding:gbkdef test(m,n):    return m,nprint(test(1,2A))

 

Python函數基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.