Python全棧__函數的初識、函數的傳回值、函數的參數

來源:互聯網
上載者:User

標籤:rip   bre   with open   功能   return   pen   print   角度   三元   

1、函數的初識

  def關鍵字 空格 函數名(與變數名命名規則相同):英文冒號

  函數體

  執行函數:函數名+()

  函數是以功能為導向的。

 

def login():    pass

 

def register():    pass

 

1 def my_len():2     l1 = [1, 2, 3, 1, 6, 9, 10]3     count = 04     for i in l1:5         count += 16 my_len()

  

2、函數的傳回值

   return:

    1、函數中遇到 return 結束函數,下面代碼不執行。

def login():    print(111)    print(222)    return    print(333)login()

 

 

 

    2、將函數裡面的值返回給函數的執行者(調用者)。

      第一種情況:

        只有return,返回None。

def login():    a = 2    b = 3    print(111)    print(a, b)    returnprint(login())

 

 

      第二種情況:

        return None

      第三種情況:

        return 單個值(返回的值與單個值的類型相同)

def login():    a = 2    b = 3    return aprint(login(), type(login()))

 

 

def login():    a = 2    b = 3    return [1, 2]print(login(), type(login()))

 

 

def login():    a = 2    b = 3    return [1, 2]ret = login()a, b = retprint(a, b)

 

 

      第四種情況:

        return 多個值(以元組的形式返回給函數的調用者)

 

def login():    a = 2    b = 3    return 1, ‘alex‘, [1, 2], {‘name‘: ‘老男孩‘}ret = login()print(ret)

 

 

  什麼是None?

  所有空集合、空列表、空字典...... ----------> None

 

def my_len():    l1 = [1, 2, 3, 1, 6, 9, 100]    count = 0    for i in l1:        count += 1    return countprint(my_len())

 

 

3、函數的參數

  1、實參角度

    1、位置參數

      一一對應,形參實參數量相等

def my_len(a):  # a 形式參數,形參    count = 0    for i in a:        count += 1    return countl1 = [1, 2, 3, 1, 6, 9, 100]print(my_len(l1))

 

 

def my_len(a):  # a 形式參數,形參    count = 0    for i in a:        count += 1    return countl1 = [1, 2, 3, 1, 6, 9, 100]my_len(l1)ret = my_len(l1)print(ret)print(my_len(l1))   # 實際參數,實參

 

 

def my_len(a):  # a 形式參數,形參    count = 0    for i in a:        count += 1    return counts1 = ‘fhdsjfdsfsadfgsd‘my_len(s1)ret = my_len(s1)print(ret)print(my_len(s1))   # 實際參數,實參

 

 

def tes(a, b, c):    print(111)    print(a, b, c)tes(22, ‘alex‘, [11, 22, 33])

 

 

  三元運算:

x = 100y = 99if x > y:    print(x)else:    print(y)

 

 

x = 100y = 99c = x if x > y else yprint(c)

 

 

def max(x, y): return x if x > y else yprint(max(3000, 200))

 

 

 

    2、關鍵字參數

      一一對應,實參形參數量相等,實參順序可變。

def func(x, y):    print(x, y)func(y=3333, x=4)

 

 

    3、混合參數

      關鍵字參數必須在位置參數的後邊。

def func(x, y, z):    print(x, y, z)func(111, 222, z=555)  # 混合傳參 位置參數必須在前邊

 

 

def func(x, y, z):    print(x, y, z)func(111, z=555, 222)

 

 

  2、形參角度

    1、位置參數

      一一對應,形參實參數量相等

def func2(y, x):    print(x, y)func2(1, 2)

 

 

    2、預設字參數

      預設參數必須在形參的位置參數後面。

      預設參數不傳值則為預設值,傳值則覆蓋預設值。

def func2(y, x, z= 1000):    print(x, y, z)func2(1, 2, 3000)

 

 

def input_information(name, sex=‘男‘):    with open(‘information‘, encoding=‘utf-8‘, mode=‘a‘) as f1:        f1.write(‘{}\t{}\n‘.format(name,sex))while True:    msg = input(‘請輸入使用者的姓名,性別\Q或者q退出‘).strip()    if msg.upper() == ‘Q‘:        break    if ‘,‘ in msg:        name1, sex1 = msg.split(‘,‘)        input_information(name1, sex1)    else:        input_information(msg)

 

 

    3、動態參數

 

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.