欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python入門教程(十九)python的函數(shù)詳解

 更新時間:2023年04月24日 08:44:16   作者:輕松學(xué)Python  
這篇文章主要介紹了Python入門教程(十九)python的函數(shù),函數(shù)是組織好的,可重復(fù)使用的,用來實(shí)現(xiàn)單一,或相關(guān)聯(lián)功能的代碼段,需要的朋友可以參考下

函數(shù)是一種僅在調(diào)用時運(yùn)行的代碼塊。

可以將數(shù)據(jù)(稱為參數(shù))傳遞到函數(shù)中。

函數(shù)可以把數(shù)據(jù)作為結(jié)果返回。

創(chuàng)建函數(shù)

在 Python 中,使用 def 關(guān)鍵字定義函數(shù):

實(shí)例

def my_function():
  print("Hello from a function")

調(diào)用函數(shù)

如需調(diào)用函數(shù),請使用函數(shù)名稱后跟括號:

實(shí)例

def my_function():
  print("Hello from a function")

my_function()

運(yùn)行實(shí)例

Hello from a function

參數(shù)

信息可以作為參數(shù)傳遞給函數(shù)。

參數(shù)在函數(shù)名后的括號內(nèi)指定。您可以根據(jù)需要添加任意數(shù)量的參數(shù),只需用逗號分隔即可。

下面的例子有一個帶參數(shù)(fname)的函數(shù)。當(dāng)調(diào)用此函數(shù)時,我們傳遞一個名字,在函數(shù)內(nèi)部使用它來打印全名:

實(shí)例

def my_function(fname):
  print(fname + " Gates")

my_function("Bill")
my_function("Steve")
my_function("Elon")

運(yùn)行實(shí)例

Rory John Gates
Jennifer Katharine Gates
Phoebe Adele Gates

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

下面的例子展示如何使用默認(rèn)參數(shù)值。

如果我們調(diào)用了不帶參數(shù)的函數(shù),則使用默認(rèn)值:

實(shí)例

def my_function(country = "China"):
  print("I am from " + country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")

運(yùn)行實(shí)例

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ā)送,它到達(dá)函數(shù)時仍將是 List(列表):

實(shí)例

def my_function(food):
  for x in food:
    print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)

運(yùn)行實(shí)例

apple
banana
cherry

返回值

如需使函數(shù)返回值,請使用 return 語句:

實(shí)例

def my_function(x):
  return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))

運(yùn)行實(shí)例

15
25
45

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

您還可以使用 key = value 語法發(fā)送參數(shù)。

參數(shù)的順序無關(guān)緊要。

實(shí)例

def my_function(child3, child2, child1):
  print("The youngest child is " + child3)

my_function(child1 = "Phoebe", child2 = "Jennifer", child3 = "Rory")

運(yùn)行實(shí)例

The youngest child is Rory

在 Python 文檔中,“關(guān)鍵字參數(shù)”一詞通常簡稱為 kwargs。

任意參數(shù)

如果您不知道將傳遞給您的函數(shù)多少個參數(shù),請在函數(shù)定義的參數(shù)名稱前添加 *。

這樣,函數(shù)將接收一個參數(shù)元組,并可以相應(yīng)地訪問各項(xiàng):

實(shí)例

如果參數(shù)數(shù)目未知,請在參數(shù)名稱前添加 *:

def my_function(*kids):
  print("The youngest child is " + kids[2])

my_function("Phoebe", "Jennifer", "Rory")

運(yùn)行實(shí)例

The youngest child is Rory

pass 語句

函數(shù)定義不能為空,但是如果您出于某種原因?qū)懥藷o內(nèi)容的函數(shù)定義,請使用 pass 語句來避免錯誤。

實(shí)例

def myfunction:
  pass

遞歸

Python 也接受函數(shù)遞歸,這意味著定義的函數(shù)能夠調(diào)用自身。

遞歸是一種常見的數(shù)學(xué)和編程概念。它意味著函數(shù)調(diào)用自身。這樣做的好處是可以循環(huán)訪問數(shù)據(jù)以達(dá)成結(jié)果。

開發(fā)人員應(yīng)該非常小心遞歸,因?yàn)樗梢院苋菀椎鼐帉懸粋€永不終止的,或者使用過量內(nèi)存或處理器能力的函數(shù)。但是,在被正確編寫后,遞歸可能是一種非常有效且數(shù)學(xué)上優(yōu)雅的編程方法。

在這個例子中,tri_recursion() 是我們定義為調(diào)用自身 (“recurse”) 的函數(shù)。我們使用 k 變量作為數(shù)據(jù),每次遞歸時遞減(-1)。當(dāng)條件不大于 0 時(比如當(dāng)它為 0 時),遞歸結(jié)束。

對于新的開發(fā)人員來說,可能需要一些時間來搞清楚其工作原理,最好的方法是測試并修改它。

實(shí)例

遞歸的例子:

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)

運(yùn)行實(shí)例

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)文章

最新評論