Python基礎-函數篇

來源:互聯網
上載者:User

標籤:函數參數   import   log   col   選擇性參數   函數   hello   blog   匯入   

 1 # 任意數量實參(選擇性參數) 2 def test(a,*b): 3     return a,b 4  5 print(test(1,2,3,4,5)) 6 # 返回的是一個元組 7  8 # 任意數量的關鍵字實參 9 def test(a,**b):10     return a,b11 12 print(test(a=1,c=2,d=3,e=4,f=5))13 # 返回的是一個字典14 15 # 讓實參變成可選的16 # 要給實參指定一個預設值,一個Null 字元串,就可以讓實參變得可選的17 def test(a=‘‘,**b):18     return a,b19 20 print(test(c=2,d=3,e=4,f=5))21 22 # 傳回值,可以是字典、列表、元組、集合函數。23 # 返回一個函數24 def test_2():25     print(‘World!‘)26 def test_1():27     print(‘Hello‘)28     return test_2()29 30 test_1()31 #返回一個集合32 def test_3():33     a = [1,1,2,2,3,4,5]34     return set(a)35 36 print(test_3())37 38 # 匯入函數各種寫法與解釋:39 40 # import module_name # 匯入整個模組41 # from module_name import function_name   # 匯入模組中的特定函數42 # from module_name import function_name as fn # 使用 as  給函數指定別名43 # import module_name as mn # 使用 as  給模組指定別名44 # from module_name import * # 匯入模組中的所有函數45 46 47 # 函數參數的順序問題:48 # 函數參數:必選參數、預設參數、選擇性參數、關鍵字參數49 def test_4(a,b=1,*c,**d):50     return a,b,c,d51 52 print(test_4(1,2,3,4,f=5))

 

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.