日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python函數(shù)

Python 包含許多內(nèi)置函數(shù)。這些函數(shù)執(zhí)行預(yù)定義的任務(wù),并且可以根據(jù)需要在任何程序中調(diào)用。但是,如果您沒有找到合適的內(nèi)置函數(shù)來滿足您的目的,您可以定義一個。我們現(xiàn)在將看到如何在 Python 程序中定義和使用函數(shù)。

成都創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),和田網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:和田等地區(qū)。和田做網(wǎng)站價(jià)格咨詢:18982081108

定義函數(shù)

函數(shù)是一個可重用的編程語句塊,用于執(zhí)行特定的任務(wù)。為了定義一個函數(shù),Python 提供了def關(guān)鍵字。以下是定義函數(shù)的語法。

Syntax:

def function_name(parameters):
    """docstring"""
    statement1
    statement2
    ...
    ...
    return [expr]

關(guān)鍵字def后面跟一個合適的標(biāo)識符作為函數(shù)的名稱和括號。一個或多個參數(shù)可以任選地在括號內(nèi)提及。括號后的:符號開始縮進(jìn)塊。

函數(shù)體中的第一個語句可以是字符串,稱為docstring。它解釋了函數(shù)/類的功能。 文檔字符串不是強(qiáng)制性的。

函數(shù)體包含一個或多個執(zhí)行某些操作的語句。也可以使用傳遞關(guān)鍵字。

可選地,函數(shù)塊中的最后一條語句是 return 語句。它將執(zhí)行控制發(fā)送回調(diào)用環(huán)境。如果在返回之前添加了一個表達(dá)式,它的值也會返回到調(diào)用代碼中。

以下示例定義了greet()函數(shù)。

Example: User-defined Function

def greet():
    """This function displays 'Hello World!'"""
    print('Hello World!') 

以上,我們已經(jīng)定義了greet()函數(shù)。第一個語句是一個 docstring,它提到了這個函數(shù)的功能。第二種類似的方法是打印方法,將指定的字符串顯示到控制臺。 注意沒有退貨單。

要調(diào)用一個已定義的函數(shù),只需在代碼中的任何地方使用它的名稱作為語句。例如,上面的函數(shù)可以用括號greet()來調(diào)用。

Example: Calling User-defined Function

greet() 

Output

Hello World! 

默認(rèn)情況下,如果返回語句不存在,所有函數(shù)都會返回None

Example: Calling User-defined Function

val = greet() 
print(val)

Output

None 

幫助()功能顯示文檔字符串,如下圖所示。

Example: Calling User-defined Function

>>> help(greet)
Help on function greet in module __main__:

    greet()
        This function displays 'Hello World!'

功能參數(shù)

可以定義一個函數(shù)來接收一個或多個參數(shù)(也稱為參數(shù)),并在功能塊內(nèi)使用它們進(jìn)行處理。參數(shù)/自變量可以被賦予合適的正式名稱。greet()函數(shù)現(xiàn)在被定義為接收名為name的字符串參數(shù)。在該功能中,print()語句被修改以顯示發(fā)送給接收參數(shù)的問候信息。

Example: Parameterized Function

def greet(name):  
    print ('Hello ', name)

greet('Steve') # calling function with argument
greet(123) 

Output

Hello Steve
Hello 123 

函數(shù)定義中使用的參數(shù)名稱為形式參數(shù)。調(diào)用函數(shù)時實(shí)際使用的對象稱為實(shí)際參數(shù)。

使用parameter:type語法,函數(shù)參數(shù)可以有注釋來指定參數(shù)的類型。例如,以下注釋了參數(shù)類型字符串。

Example: Parameterized Function

def greet(name:str):  
    print ('Hello ', name)

greet('Steve') # calling function with string argument
greet(123) # raise an error for int argument 

多個參數(shù)

一個函數(shù)可以有多個參數(shù)。下面的函數(shù)接受三個參數(shù)。

Example: Parameterized Function

def greet(name1, name2, name3):  
    print ('Hello ', name1, ' , ', name2 , ', and ', name3)

greet('Steve', 'Bill', 'Yash') # calling function with string argument 

Output

Hello Steve, Bill, and Yash 

未知的參數(shù)數(shù)量

如果您不知道用戶將要傳遞的參數(shù)數(shù)量,Python 中的函數(shù)可以通過將*放在參數(shù)之前來獲得未知數(shù)量的參數(shù)。

Example: Parameterized Function

def greet(*names):  
    print ('Hello ', names[0], ', ', names[1], ', ', names[3])

greet('Steve', 'Bill', 'Yash') 

Output

Hello Steve, Bill, and Yash 

下面的函數(shù)可以處理任意數(shù)量的參數(shù)。

Example: Parameterized Function

def greet(*names):
    i=0
    print('Hello ', end='')
    while len(names) > i:
        print(names[i], end=', ')
        i+=1

greet('Steve', 'Bill', 'Yash') 
greet('Steve', 'Bill', 'Yash', 'Kapil', 'John', 'Amir') 

Output

Hello Steve, Bill, Yash,
Hello Steve, Bill, Yash, Kapil, John, Amir 

帶有關(guān)鍵字參數(shù)的函數(shù)

為了調(diào)用帶有參數(shù)的函數(shù),必須提供相同數(shù)量的實(shí)際參數(shù)。但是,可以通過以任何順序使用參數(shù)名傳遞參數(shù)值來調(diào)用函數(shù)。例如,下面使用參數(shù)名傳遞值。

def greet(firstname, lastname):
    print ('Hello', firstname, lastname)

greet(lastname='Jobs', firstname='Steve') # passing parameters in any order using keyword argument 

Output

Hello Steve Jobs 

關(guān)鍵字參數(shù)**kwarg

該函數(shù)可以有一個前綴為**的參數(shù)。這種類型的參數(shù)初始化為新的有序映射,接收任何多余的關(guān)鍵字參數(shù),默認(rèn)為相同類型的新空映射。

Example: Parameterized Function

def greet(**person):
    print('Hello ', person['firstname'],  person['lastname'])

greet(firstname='Steve', lastname='Jobs')
greet(lastname='Jobs', firstname='Steve')
greet(firstname='Bill', lastname='Gates', age=55) 
greet(firstname='Bill') # raises KeyError 

Output

Hello Steve Jobs
Hello Steve Jobs
Hello Bill Gates 

使用**參數(shù)時,參數(shù)的順序無關(guān)緊要。但是,參數(shù)的名稱必須相同。 使用paramter_name['keyword_argument']訪問關(guān)鍵字參數(shù)的值。

如果函數(shù)訪問關(guān)鍵字參數(shù),但是調(diào)用代碼沒有傳遞該關(guān)鍵字參數(shù),那么它將引發(fā)KeyError異常,如下所示。

Example: Parameterized Function

def greet(**person):
    print('Hello ', person['firstname'],  person['lastname'])

greet(firstname='Bill') # raises KeyError, must provide 'lastname' arguement 

Output

Traceback (most recent call last):
  File "", line 1, in 
    greet(firstname='Bill')
  File "", line 2, in greet
    print('Hello ', person['firstname'],  person['lastname'])
KeyError: 'lastname' 

帶默認(rèn)值的參數(shù)

定義函數(shù)時,可以為其參數(shù)指定默認(rèn)值。如果在調(diào)用函數(shù)時傳遞了適當(dāng)?shù)膶?shí)際參數(shù),則該默認(rèn)值將被替換。但是,如果沒有提供實(shí)際參數(shù),將在函數(shù)內(nèi)部使用默認(rèn)值。

下面的greet()函數(shù)是用具有默認(rèn)值'Guest'name參數(shù)定義的。 只有通過一些實(shí)際的參數(shù),才會被替換。

Example: Parameter with Default Value

def greet(name = 'Guest'):
    print ('Hello', name)

greet()
greet('Steve') 

Output

Hello Guest
Hello Steve 

帶返回值的函數(shù)

大多數(shù)時候,我們需要函數(shù)的結(jié)果用于進(jìn)一步的處理。因此,當(dāng)函數(shù)返回時,它也應(yīng)該返回值。

用戶定義的函數(shù)也可以通過在 return 語句前放置一個表達(dá)式來向調(diào)用環(huán)境返回值。在這種情況下,返回值必須賦給某個變量。

Example: Return Value

def sum(a, b): 
    return a + b 

可以調(diào)用上面的函數(shù)并提供值,如下所示。

Example: Parameter with Default Value

total=sum(10, 20) 
print(total)
total=sum(5, sum(10, 20))
print(total) 

Output

30
35 

分享名稱:Python函數(shù)
網(wǎng)站網(wǎng)址:http://www.dlmjj.cn/article/dpoiojj.html