Python入門教程(十九)python的函數(shù)詳解
函數(shù)是一種僅在調(diào)用時運行的代碼塊。
可以將數(shù)據(jù)(稱為參數(shù))傳遞到函數(shù)中。
函數(shù)可以把數(shù)據(jù)作為結(jié)果返回。
創(chuàng)建函數(shù)
在 Python 中,使用 def 關(guān)鍵字定義函數(shù):
實例
def my_function():
print("Hello from a function")
調(diào)用函數(shù)
如需調(diào)用函數(shù),請使用函數(shù)名稱后跟括號:
實例
def my_function():
print("Hello from a function")
my_function()
運行實例
Hello from a function
參數(shù)
信息可以作為參數(shù)傳遞給函數(shù)。
參數(shù)在函數(shù)名后的括號內(nèi)指定。您可以根據(jù)需要添加任意數(shù)量的參數(shù),只需用逗號分隔即可。
下面的例子有一個帶參數(shù)(fname)的函數(shù)。當調(diào)用此函數(shù)時,我們傳遞一個名字,在函數(shù)內(nèi)部使用它來打印全名:
實例
def my_function(fname):
print(fname + " Gates")
my_function("Bill")
my_function("Steve")
my_function("Elon")
運行實例
Rory John Gates Jennifer Katharine Gates Phoebe Adele Gates
默認參數(shù)值
下面的例子展示如何使用默認參數(shù)值。
如果我們調(diào)用了不帶參數(shù)的函數(shù),則使用默認值:
實例
def my_function(country = "China"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
運行實例
I am from Sweden I am from India I am from China I am from Brazil
以 List 傳參
您發(fā)送到函數(shù)的參數(shù)可以是任何數(shù)據(jù)類型(字符串、數(shù)字、列表、字典等),并且在函數(shù)內(nèi)其將被視為相同數(shù)據(jù)類型。
例如,如果您將 List 作為參數(shù)發(fā)送,它到達函數(shù)時仍將是 List(列表):
實例
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
運行實例
apple banana cherry
返回值
如需使函數(shù)返回值,請使用 return 語句:
實例
def my_function(x): return 5 * x print(my_function(3)) print(my_function(5)) print(my_function(9))
運行實例
15 25 45
關(guān)鍵字參數(shù)
您還可以使用 key = value 語法發(fā)送參數(shù)。
參數(shù)的順序無關(guān)緊要。
實例
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Phoebe", child2 = "Jennifer", child3 = "Rory")
運行實例
The youngest child is Rory
在 Python 文檔中,“關(guān)鍵字參數(shù)”一詞通常簡稱為 kwargs。
任意參數(shù)
如果您不知道將傳遞給您的函數(shù)多少個參數(shù),請在函數(shù)定義的參數(shù)名稱前添加 *。
這樣,函數(shù)將接收一個參數(shù)元組,并可以相應(yīng)地訪問各項:
實例
如果參數(shù)數(shù)目未知,請在參數(shù)名稱前添加 *:
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Phoebe", "Jennifer", "Rory")
運行實例
The youngest child is Rory
pass 語句
函數(shù)定義不能為空,但是如果您出于某種原因?qū)懥藷o內(nèi)容的函數(shù)定義,請使用 pass 語句來避免錯誤。
實例
def myfunction: pass
遞歸
Python 也接受函數(shù)遞歸,這意味著定義的函數(shù)能夠調(diào)用自身。
遞歸是一種常見的數(shù)學(xué)和編程概念。它意味著函數(shù)調(diào)用自身。這樣做的好處是可以循環(huán)訪問數(shù)據(jù)以達成結(jié)果。
開發(fā)人員應(yīng)該非常小心遞歸,因為它可以很容易地編寫一個永不終止的,或者使用過量內(nèi)存或處理器能力的函數(shù)。但是,在被正確編寫后,遞歸可能是一種非常有效且數(shù)學(xué)上優(yōu)雅的編程方法。
在這個例子中,tri_recursion() 是我們定義為調(diào)用自身 (“recurse”) 的函數(shù)。我們使用 k 變量作為數(shù)據(jù),每次遞歸時遞減(-1)。當條件不大于 0 時(比如當它為 0 時),遞歸結(jié)束。
對于新的開發(fā)人員來說,可能需要一些時間來搞清楚其工作原理,最好的方法是測試并修改它。
實例
遞歸的例子:
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(6)
運行實例
Recursion Example Results 1 3 6 10 15 21
到此這篇關(guān)于Python入門教程(十九)python的函數(shù)詳解的文章就介紹到這了,更多相關(guān)python的函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
正則給header的冒號兩邊參數(shù)添加單引號(Python請求用)
這篇文章主要介紹了正則給header的冒號兩邊參數(shù)添加單引號(Python請求用)的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
pytorch中permute()函數(shù)用法實例詳解
permute中參數(shù)為張量的維度,將不同維度以不同的維度排列,得到一個新的張量,在深度學(xué)習(xí)中的主要作用是將特征值聚類,下面這篇文章主要給大家介紹了關(guān)于pytorch中permute()函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2022-04-04

