日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

在python中、正确的函数定义格式为_Python函数的定义与实现

發(fā)布時(shí)間:2024/8/23 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在python中、正确的函数定义格式为_Python函数的定义与实现 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 函數(shù)的介紹

函數(shù)是 實(shí)現(xiàn)具有特定功能的代碼塊

Python中預(yù)制了許多內(nèi)置函數(shù),也可以根據(jù)自己的需求創(chuàng)建自定義的函數(shù)

隱藏實(shí)現(xiàn)功能的細(xì)節(jié)

代碼的復(fù)用

提高可讀性,便與調(diào)試

def 函數(shù)名(形參1, 形參2...):

要運(yùn)行的代碼塊(函數(shù)體)

return 返回的數(shù)據(jù)(返回值)

函數(shù)名要有描述性,建議使用下劃線的格式: 動(dòng)作_描述()

在函數(shù)體內(nèi)定義的變量是局部變量,只能作用于該函數(shù)

2. 形參和實(shí)參

2.1 形參/實(shí)參介紹

# 定義一個(gè)簡(jiǎn)單的問(wèn)候函數(shù)

def print_hello(username):

print("hello,{}".format(username))

# 調(diào)用函數(shù),傳入?yún)?shù)

print_hello("world")

-- run --

hello,world

以上示例中,變量 username是一個(gè)形參 :函數(shù)完成其工作所需的一項(xiàng)信息,在調(diào)用函數(shù)中,值 "world"是一個(gè)實(shí)參 :實(shí)參是調(diào)用函數(shù)時(shí)傳遞給函數(shù)的信息,將實(shí)參傳遞給函數(shù) print_hello() ,這個(gè)值被存儲(chǔ)在形參 username 中

函數(shù)定義中可能包含多個(gè)形參,因此函數(shù)調(diào)用中也可能包含多個(gè)實(shí)參,接下來(lái)會(huì)介紹向函數(shù)傳遞實(shí)參的方式

2.2 位置實(shí)參

位置實(shí)參:要求實(shí)參的順序與形參的順序?qū)?yīng)

定義:def 函數(shù)名(形參1, 形參2, 形參3):

調(diào)用:函數(shù)名(實(shí)參1, 實(shí)參2, 實(shí)參3)

位置實(shí)參的順序很重要,如果實(shí)參的順序與形參不相同,最終的函數(shù)結(jié)果可能出乎意料

# 定義一個(gè)寵物信息函數(shù)

def pet_describe(pet_type, pet_name):

"顯示寵物類(lèi)型和名字"

print("我的寵物是一只{},它的名字是{}".format(pet_type, pet_name))

# 實(shí)參與形參順序相同

pet_describe("貓", "大橘")

# 實(shí)參與形參的順序不同

pet_describe("大橘", "貓")

-- run --

我的寵物是一只貓,它的名字是大橘

我的寵物是一只大橘,它的名字是貓

2.3 關(guān)鍵字實(shí)參

關(guān)鍵字實(shí)參:每個(gè)實(shí)參都由變量名和值組成

定義:def 函數(shù)名(形參1, 形參2, 形參3):

調(diào)用:函數(shù)名(形參2=實(shí)參2, 形參3=實(shí)參3, 形參1=實(shí)參1)

關(guān)鍵字實(shí)參是傳遞給函數(shù)的 名稱(chēng)-值 對(duì),直接在實(shí)參中將名稱(chēng)和值關(guān)聯(lián)起來(lái),因此無(wú)需考慮函數(shù)調(diào)用中的實(shí)參順序

# 接著用上面位置實(shí)參的示例

def pet_describe(pet_type, pet_name):

"顯示寵物類(lèi)型和名字"

print("我的寵物是一只{},它的名字是{}".format(pet_type, pet_name))

# 明確指出了各個(gè)實(shí)參對(duì)應(yīng)的形參,無(wú)需關(guān)注參數(shù)順序

pet_describe(pet_type="貓", pet_name="大橘")

pet_describe(pet_name="大橘", pet_type="貓")

-- run --

我的寵物是一只貓,它的名字是大橘

我的寵物是一只貓,它的名字是大橘

使用關(guān)鍵字實(shí)參時(shí),務(wù)必準(zhǔn)確的指定函數(shù)定義中的形參名

2.4 混合傳參

使用位置實(shí)參與關(guān)鍵字實(shí)參混合使用的方式

使用混合傳參時(shí),關(guān)鍵字參數(shù)必須在位置參數(shù)后面

# 位置參數(shù)要放在最前面

def pet_describe(pet_type, pet_name, age):

"顯示寵物類(lèi)型和名字"

print("我的寵物是一只{},它的名字是{},今年{}歲".format(pet_type, pet_name, age))

pet_describe("貓", age=1, pet_name="大橘")

-- run --

我的寵物是一只貓,它的名字是大橘,今年1歲

2.5 限制位置實(shí)參

定義:def 函數(shù)名(形參1, *, 形參2, 形參3):

調(diào)用:函數(shù)名(實(shí)參1, 形參2=實(shí)參2, 形參3=實(shí)參3)

星號(hào) * 代表之后所有參數(shù)傳參時(shí) 必須使用關(guān)鍵字傳參

# *號(hào)之后的都必須使用關(guān)鍵字傳參法,否則報(bào)錯(cuò)

def pet_describe(pet_type, *, pet_name, age):

"顯示寵物類(lèi)型和名字"

print("我的寵物是一只{},它的名字是{},今年{}歲".format(pet_type, pet_name, age))

pet_describe("貓", "大橘", 1)

-- run --

Traceback (most recent call last):

File "D:\Python\imooc\2.Python函數(shù)與模塊\2.1.函數(shù)的定義與實(shí)現(xiàn)\2.1.1.function\test.py", line 5, in

pet_describe("貓", "大橘", 1)

TypeError: pet_describe() takes 1 positional argument but 3 were given

# 類(lèi)型錯(cuò)誤:pet_describe()接受1個(gè)位置參數(shù),但給出了3個(gè)

2.6 設(shè)置參數(shù)的默認(rèn)值

定義:def 函數(shù)名(形參=默認(rèn)值):

在定義函數(shù)時(shí),可以根據(jù)需要給指定的形參設(shè)置默認(rèn)值

在調(diào)用函數(shù)中如果給該形參提供了實(shí)參,則使用指定的實(shí)參值,否則使用形參的默認(rèn)值

設(shè)置默認(rèn)值時(shí),在形參列表中必須 先列出沒(méi)有默認(rèn)值的形參 ,再列出有默認(rèn)值的形參 ,這讓Python依然能夠正確的解讀位置實(shí)參

# 給設(shè)置了默認(rèn)值的形參指定實(shí)參時(shí),也可以用位置實(shí)參

def pet_describe(pet_name, age, pet_type="貓"):

"顯示寵物類(lèi)型和名字"

print("我的寵物是一只{},它的名字是{},今年{}歲".format(pet_type, pet_name, age))

pet_describe("大橘", 1)

pet_describe("大白", 0.5, "狗")

-- run --

我的寵物是一只貓,它的名字是大橘,今年1歲

我的寵物是一只狗,它的名字是大白,今年0.5歲

定義:def 函數(shù)名(形參=''):

通過(guò)給形參設(shè)置空字符串的默認(rèn)值后,實(shí)參就變成可選的了,這樣使用函數(shù)就只需要在必要時(shí)才提供額外的信息

# 姓 + 名 + 中間名(可選)組成完整的名字

def get_formatted_name(first_name, last_name, middle_name=''):

"返回完整的姓名"

if middle_name:

full_name = "{} {} {}".format(first_name, middle_name, last_name)

else:

full_name = "{} {}".format(first_name, last_name)

return full_name.title()

print(get_formatted_name("jimi", "hendrix"))

print(get_formatted_name("john", "hooker", "lee"))

-- run --

Jimi Hendrix

John Lee Hooker

2.7 使用序列傳參

調(diào)用:函數(shù)名(*序列名)

序列不是Python中的某一種數(shù)據(jù)類(lèi)型,而是一種數(shù)據(jù)結(jié)構(gòu)的統(tǒng)稱(chēng),如: 列表、元組、數(shù)字序列、字符串等 都稱(chēng)為序列,具體請(qǐng)移步 Python序列

# 定義一個(gè)簡(jiǎn)單的數(shù)學(xué)計(jì)算函數(shù)

def calc(a, b, c):

d = (a + b) * c

return d

# 位置傳參

print(calc(1, 5, 10))

# 使用列表序列傳參

num_list1 = [2, 4, 6]

print(calc(*num_list1))

-- run --

60

36

# 函數(shù)內(nèi)定義了遍歷語(yǔ)句,所以列表元素是依次傳入,不需要加*

def greet_users(names):

"問(wèn)候列表中的每個(gè)用戶(hù)"

for name in names:

print("Hello,{}".format(name))

username = ["張三", "李四", "王五"]

greet_users(username)

-- run --

Hello,張三

Hello,李四

Hello,王五

2.8 使用字典傳參

調(diào)用:函數(shù)名(**字典名)

使用字典傳參時(shí),類(lèi)似于關(guān)鍵字傳參,字典key的排列順序無(wú)需對(duì)應(yīng)形參的順序,但是兩者的名稱(chēng)、數(shù)量必須一致

# 定義一個(gè)簡(jiǎn)單員工信息函數(shù)

def fun_dict(name, hiredate, tel, dept):

print("{p1}隸屬于{p4},電話(huà):{p3},入職如期:{p2}".format(p1=name, p2=hiredate, p3=tel, p4=dept))

# 字典傳參,key的名稱(chēng)要和形參對(duì)上,key的數(shù)量也要和形參數(shù)一致

dict1 = {'name':'張三','hiredate':'2020-06-27','dept':'技術(shù)部','tel':13012345678}

fun_dict(**dict1)

-- run --

張三隸屬于技術(shù)部,電話(huà):13012345678,入職如期:2020-06-27

3. 函數(shù)的返回值

3.1 函數(shù)中的return

參數(shù)是函數(shù)的輸入數(shù)據(jù),而返回值則是函數(shù)的輸出結(jié)果

return 不是必須的,return語(yǔ)句 執(zhí)行后,函數(shù)將中斷執(zhí)行

return解釋.png

無(wú)return的函數(shù).png

return與print區(qū)別.png

上述回答來(lái)自知乎用戶(hù)“sqybi”,個(gè)人覺(jué)得通俗易懂,就摘錄下來(lái)了,感謝大佬的解惑!

# 示例1:print

def print_hello():

print("hello")

print_hello()

str1 = print_hello()

print(str1)

-- run --

hello

hello

None

# 示例2:return

def print_hello():

return "hello"

print_hello()

print(print_hello())

str2 = print_hello()

print(str2)

-- run --

hello

hello

上述示例中,print是將結(jié)果打印在標(biāo)準(zhǔn)輸出(屏幕)上,所以直接調(diào)用函數(shù)名就能執(zhí)行打印;return是將結(jié)果保存至內(nèi)存,打印時(shí)需要借助print函數(shù)

使用print的函數(shù)賦值時(shí),由于沒(méi)有返回值,僅有打印屏幕的操作,所以賦值變量返回的是 None ;而使用return的函數(shù)賦值時(shí),將保存在內(nèi)存中的返回值賦值給變量,所以賦值變量有返回值

3.2 返回值包含多個(gè)數(shù)據(jù)

在函數(shù)內(nèi)定義嵌套字典,調(diào)用函數(shù)時(shí)使用不同的索引就可以獲取多個(gè)返回?cái)?shù)據(jù)

# 定義一個(gè)包含公司內(nèi)信息的嵌套字典函數(shù)(僅列舉部分信息)

def get_detail_info():

dict1 = {

"employee": [

{"name": "張三", "salary": 3000},

{"name": "李四", "salary": 4000}

],

"device": [

{"id": "88888888", "title": "xx筆記本"},

{"id": "66666666", "title": "xx臺(tái)式機(jī)"}

],

"...": [{}, {}],

"......": [{}, {}]

}

return dict1

# 獲取員工張三的薪資信息

d = get_detail_info()

sal = d.get("employee")[0].get("salary")

print(sal)

# 獲取xx臺(tái)式機(jī)設(shè)備的id號(hào)

dev = d.get("device")[1].get("id")

print(dev)

-- run --

3000

66666666

總結(jié)

以上是生活随笔為你收集整理的在python中、正确的函数定义格式为_Python函数的定义与实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。