真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

python拆包和封包

"""
python的拆包和封包之 *號在函數(shù)形參和實參的區(qū)別
1. 在函數(shù)形參定義時添加*就是封包過程,封包默認是以元組形式進行封包
2. 在函數(shù)實參調用過程添加*就是拆包過程,拆包過程中會報列表或者元組拆成單個元素
"""

subject = ["math", "chinese", 'english', 'physics', 'history']


def print_subject_one(sub):
print(*sub)


def print_subject_two(*sub):
print(sub)


def print_subject_three(*sub):
print(*sub)


def print_subject_four(*sub):
print(*sub)


def print_subject_five(*sub):
print(sub)


print_subject_one(subject) # math chinese english physics history
print_subject_two(subject) # (['math', 'chinese', 'english', 'physics', 'history'],)
print_subject_three(subject) # ['math', 'chinese', 'english', 'physics', 'history']
print_subject_four(*subject) # math chinese english physics history
print_subject_five(*subject) # ('math', 'chinese', 'english', 'physics', 'history')


"""
python的拆包和封包之 **號在函數(shù)形參和實參的區(qū)別
1. 在函數(shù)形參定義時添加**就是封包過程,封包默認是字典形式進行封包,
2. 在函數(shù)實參調用過程添加**就是拆包過程,通常在存在關鍵字參數(shù)的函數(shù)去使用**對字典進行拆包
"""

hobby = {"jack": "dance", "henry": "basketball", "jenny": "swimming", "richard": "reading"}


def print_hobby_one(**bby):
print(bby)


def print_hobby_two(**bby):
print(bby)


def print_hobby_three(**bby):
if "jack" in bby:
print('jack like dance')
if "jenny" in bby:
print("jenny like swimming")


print_hobby_one(**hobby) # {'jack': 'dance', 'henry': 'basketball', 'jenny': 'swimming', 'richard': 'reading'}
print_hobby_two(jack='dance', henry='basketball') # {'jack': 'dance', 'henry': 'basketball'}
print_hobby_three(**hobby) # jack like dance jenny like swimming


"""
python的拆包和封包之 * 在其它場景的應用
"""

a, *_, c = [1, 2, 3, 4, 5, 6] # a=1, c=6
print(a) # a=1, c=6
print(c) # a=1, c=6


a, b, *_ = [1, 2, 3, 4, 5, 6]
print(a) # a=1
print(b) # b=2


# 一行代碼搞定兩個變量交換賦值
x = 10
y = 20

x, y = y, x
print(x) # x = 20
print(y) # y = 10


網頁名稱:python拆包和封包
URL鏈接:http://weahome.cn/article/dsogjid.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部