Python之函數初識

來源:互聯網
上載者:User

標籤:name   可重複   code   open   aaa   混合   encoding   adf   put   

一、函數

代碼重複使用,代碼可讀性差,所以要使用函數。

函數是組織好的,可重複使用的,用來實現單一,或相關聯功能的程式碼片段。

二、函數的定義與調用

1 def my_len():  #def 關鍵字,定義函數, 空格 函數名(變數)2     l1 = [1, 2, 3, 4, 5, 6, 5, 4, 3]3     count = 04     for i in l1:5         count += 16 7 my_len()  # 函數名 + () 執行此函數8 9 #函數是以功能為導向,函數內部盡量不要有print

三、函數的傳回值

3.1 遇到return 此函數結束,不在向下進行

1 def my_len():2     l1 = [1, 2, 3, 4, 5, 6, 5, 4, 3]3     print(1111)4     print(222)5     return6     print(333)

3.2 return傳回值

3.2.1 不寫return,返回None

1 def my_len():2     l1 = [1, 2, 3, 4, 5, 6, 5, 4, 3]3     count = 04     for i in l1:5         count += 16 print(my_len())
View Code

3.2.2 return None ,返回None

1 def my_len():2     l1 = [1,2,3,4,5,6,7]3     count = 04     for i in l1:5         count += 16     return None7 print(my_len())
View Code

 3.2.3 return 單個值 返回此單個值

def my_len():    l1 = [1,2,3,4]    count = 0     for i in l1:        count += 1    return ‘alex‘print(my_len())

3.2.4 return 多個值 將多個值包在元組中,返回給調用者此元組

 1 def my_len(): 2     l1 = [1, 2, 3, 4, 5, 6, 5, 4, 3] 3     count = 0 4     for i in l1: 5         count += 1 6     return 1,2,[123,2] 7 ret = my_len() 8 print(ret) 9 def my_len():10     l1 = [1, 2, 3, 4, 5, 6, 5, 4, 3]11     count = 012     for i in l1:13         count += 114     return 1,2,[123,2]15 ret1,ret2,ret3 = my_len()16 print(ret1,ret2,ret3)
View Code

四、函數的參數

4.1 函數的傳參

 1 l2 = [1, 2, 3, 4, 5, 6, 5, 4, 3, 3, ‘alex‘] 2 #函數的傳參 3 def my_len(x):  #形參:形式參數 4     count = 0 5     for i in x: 6         count += 1 7     return count 8 ret = my_len(l2)  # 實參:實際參數 9 print(ret)10 len(s)

4.2 實參傳參

4.2.1 按位置傳參

1 def my_len(x,y,z):  #形參:形式參數2     count = 03     for i in x:4         count += 15     return count6 ret = my_len(l2,s,‘alex‘)  # 按位置傳參7 print(ret)

4.2.2 按關鍵字傳參

1 def func(x,y):2     z = x if x > y else y3     return z4 func(y = 2,x = 3) #關鍵字傳參

4.2.3 混合傳參 關鍵字參數永遠在位置參數後面

1 def func(x,y,a,b):2     print(x,y)3     print(a,b)4 func(1,2,b = 34,a = 3) #  混合傳參

4.3 形式傳參

4.3.1 位置參數

def my_len(x,y):  #形參:形式參數    count = 0    for i in x:        count += 1    return countret = my_len(2,3)  # 按位置傳參print(ret)

4.3.2 預設參數

def func(name,sex=‘男‘):    with open(‘name_list‘,‘a‘,encoding=‘utf-8‘) as f1:        f1.write(‘{}  {}\n‘.format(name,sex))while True:    name = input(‘請輸入名字:‘)    if ‘姐‘ in name:        sex = input(‘請輸入性別:‘)        func(name,sex) #  混合    else:        func(name)

 

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.